Introduction to Programming Java Lab 2: Variables and Number Types 1 JavaLab2 lecture slides.ppt...
-
Upload
robert-morse -
Category
Documents
-
view
216 -
download
0
Transcript of Introduction to Programming Java Lab 2: Variables and Number Types 1 JavaLab2 lecture slides.ppt...
Introduction to Programming
Java Lab 2:Variables and Number Types
1JavaLab2 lecture slides.ppt
Ping Brennan ([email protected])
Two Java Projects
Project Name: JavaLab2a
2
Project Name: JavaLab2b
Volume1
Purchase Price(fixed prices)
Purchase Price using keyboard
input(input from Keyboard)
SwapCharacters
JavaLab2a: Class Volume1
• Compute the volume in litres of a six-pack of soda cans.
• Objective – declare variables in the method main using
the two statements below. int cansPerPack = 6; double canVolume = 0.355;
Note: All variables in Java programs must be declared.
3
JavaLab2a: Class Volume1 (2)
• Data types– integer
The variable cansPerPack is of type int and the value of the variable is set to 6.
– double (floating point) The variable canVolume can take non-integer values and is declared as double.
• Use the formula below inside a System.out.print() statement to calculate the total volume.
4
cansPerPack * canVolume
JavaLab2a: Class Volume1 (3)
• Type in the following code for the class Volume1
5
/*** This program computes the volume (in litres) of a six-pack of soda cans.*/ public class Volume1 { public static void main(String[] args) {
int cansPerPack = 6;
double canVolume = 0.355; // Litres in a 12-ounce can
System.out.print("A six-pack of 12-ounce cans contains ");
System.out.print(cansPerPack * canVolume);
System.out.println(" litres.");
}
}
JavaLab2a: Calculate total purchase price
• Revise the method main in class Volume1 to calculate total purchase price using fixed variables.
• Objective – declare two new variables: unitPrice which is of type double; and quantity which is of type int.
– Use reasonable initial values for the two variables, e.g. int quantity = 6;
• Use the formula below to calculate total purchase price
6
unitPrice * quantity;
JavaLab2a: Input from Key Board
• Revise the method main in class Volume1 to calculate total purchase price using input data from keyboard.
• Objectives – declare variables– read data from keyboard
• Use the formula below to calculate total purchase price
7
unitPrice * quantity;
JavaLab2a: Input from Key Board (2)
• The following lines of code are required.
• Note: input from the key board is made in the BlueJ: Terminal Window – JavaLab2a. You must first create this window using a print or println statement (as shown above) before attempting to enter input from the key board.
8
import java.util.Scanner; // the first line of program file
// The following code must be written inside the method mainScanner in = new Scanner(System.in); /* create an object in of type Scanner */System.out.print("Enter the quantity: "); // create terminal window
int quantity = in.nextInt();/* read an integer and store it in the variable quantity. */
/* The object in contains the method in.nextInt() for reading integers from the key board. */
JavaLab2b: Class SwapCharacters
• String exercise on swapping two letters in a string.
• Objectives – String type– Concatenation– Strings and characters– Substrings
• Exercises– Concatenate strings– Working with substrings
9
String operations
String str = "Ja"; // Declare the string variable str. Set it to "Ja"
str = str + "va"; // ‘+’ specifies string concatenation. // str is set to "Java"
String greeting = "H & S";int n = greeting.length();/* The method greeting.length returns the length of the
string, ‘greeting’. In this case n is set to 5 */
10
String operations (2)
String str = "Sally";String str2 = str.substring(1, 4);/* Extract from str the substring starting at position 1 and
ending immediately before position 4. Set str2 equal to this substring (i.e. str2 now has the value "all"). Note that string indexing begins with 0. */
String str = "Sally";String str3 = str.substring(1); /* str3 now has the value
"ally" */
11
Swapping two letters in a string
1. Given a string, for example, String str ="Harry Potter";
2. Input position i = 2 (so str.substring(i,i+1) returns the character r)
3. Input position j = 8 (so str.substring(j,j+1) returns t)
4. String first = str.substring(0, i); /* returns Ha */
5. String middle = str.substring(i+1, j); /* returns ry Po */
6. String last = str.substring(j+1); /* returns ter */
7. Concatenate five strings:first + str.substring(j,j+1) + middle + str.substring(i,i+1) + last which returns “Hatry Porter” 12
H a r r y P o t t e r
0 1 2 3 4 5 6 7 8 9 10
11index
Swapping two letters in a string
(alternative solution)1. Given a string, for example, String str ="Harry Potter";
2. Input position i = 2 (so str.charAt(i) returns the character r)
3. Input position j = 8 (so str.charAt(j) returns t)
4. String first = str.substring(0, i); /* returns Ha */
5. String middle = str.substring(i+1, j); /* returns ry Po */
6. String last = str.substring(j+1); /* returns ter */
7. Concatenate five strings:first + str.charAt(j) + middle + str.charAt(i) + last
which returns “Hatry Porter” 13
H a r r y P o t t e r
0 1 2 3 4 5 6 7 8 9 10
11index