Uploaded by Albard Espinoza

Discussion 02 CSE11

advertisement
CSE11
Accelerated Intro to Programming
Discussion 2
Field Access
class DiscussionExamples{
int pricePerGallon = 4;
int gallons1 = 2;
double gallons2 = 5;
double gallons3 = 5.2;
}
double price1 = this.pricePerGallon * this.gallons1;
//8.0
double price2 = this.pricePerGallon * this.gallons2;
//20.0
double price3 = this.pricePerGallon * this.gallons3;
//20.8
Method
class DiscussionExamples{
int pricePerGallon = 4;
// calculate oil price
double computePrice(double gallons){
return this.pricePerGallon * gallons;
}
double result1 = this.computePrice(2);
//8.0
double result2 = this.computePrice(5);
//20.0
double result3 = this.computePrice(5.2);
}
//20.8
Design Recipe
1.
Determine the input and output types, and name the parameters and method.
2.
Write the method header and default return value
3.
Write a comment describing what the method will produce.
4.
Write several examples of calling the method with different arguments
5.
Fill in the body of the method
6.
Run the method to test it out, and check that the output of the examples is correct.
Class String
●
String value given between double quotes
●
Characters including digits, space
class DiscussionExamples{
String department = "CSE";
String courseNumber = "11";
}
String courseNumber = 11;
int courseNumber = “11”;
Error: incompatible types
Class String
●
String can include space, special symbols (!, @, #, …)
●
Case-sensitive
class DiscussionExamples{
String greeting = “Hello, world!”;
String email = “java@ucsd.edu” ;
String classUpper = “CSE”;
String classLower = “cse”;
}
String Concatenation
●
+ operator is used to add strings
class DiscussionExamples{
String s1 = “My favorite programming language is ”;
String s2 = “Java”;
String s = this.s1 + this.s2 + “.”;
}
String Concatenation
●
If one operand is String, the other operand is converted to String
class DiscussionExamples{
String myString = “The number of students: ” + 100;
}
int converted to String
String Concatenation
●
If one operand is String, the other operand is converted to String
class DiscussionExamples{
String myString = 5 + 3 + “String” + 4 + 4;
}
5+3 → 8
8+“String” → ”8”+“String” → “8String”
“8String” + 4 → “8String” + “4” → “8String4”
“8String4” + 4 → “8String4” + “4” → “8String44”
String Methods
●
int length() : Return the number of characters in the string
●
int indexOf(String) : Produce the index at which the input string appears
●
String substring(int, int) : Return the portion of the string
●
String replace(String, String) : Find a string and replace it with the other
●
String repeat(int) : produce copies of the string
String Methods – length
●
int length() : Return the number of characters in the string
●
A space or a symbol as well as a letter is counted as a character
class DiscussionExamples{
String myString = “Learning Java is fun!”;
int myNum = myString.length();
}
Learning Java is fun!
0 1 2 3 4 5 6 7 8 9 10 1112 13 1415 1617 18 19 20
String Methods – indexOf
●
int indexOf(String) : Take a string as an input
●
Produce the index at which the input string appears in the string
class DiscussionExamples{
String myString = “Learning Java is fun!”;
int idxLearning = myString.indexOf(“Learning”);
int idxJava = myString.indexOf(“Java”);
int idxFun = myString.indexOf(“Fun”);
}
doesn’t
exist.
return -1
String Methods – substring
●
String substring(int, int) : Return a part of the string
●
The starting index is inclusive, while the ending index is exclusive
class DiscussionExamples{
String myString = “Learning Java is fun!”;
String s1 = myString.substring(2, 5);
String s2 = myString.substring(5, 2);
String s3 = myString.substring(17, 23);
String s4 = myString.substring(-1, 3);
String s5 = myString.substring(2, 2);
}
Range out of
bounds for length 21
String Methods – replace, repeat
●
String replace(String, String) : Find a string and replace it with the other
●
String repeat(int) : produce copies of the string
class DiscussionExamples{
String myString = “Welcome to cse11. ”;
String s1 = myString.replace(“cse11”, “CSE11”);
String s2 = s1.repeat(2);
}
Design Recipe for a Method with Strings
Create a method that returns the initial of a full name (assume no middle name in input)
class DiscussionExamples{
// return the initial for a full name given
String printInitial(String fullName){
String firstName = fullName.substring(0, 1);
int idxSpace = fullName.indexOf(“ ”);
String lastName = fullName.substring(idxSpace + 1, idxSpace + 2);
return firstName + lastName;
}
String ex1 = printInitial(“Ada Lovelace”);
//“AL”
String ex2 = printInitial(“Alan Turing”);
//“AT”
String ex3 = printInitial(“Tim Berners-Lee”);
}
//“TB”
PA2 is due 10PM, Jan. 24 (Tuesday)
Download