Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Programming Java Lab 2: Variables and Number Types 1 JavaLab2 lecture slides.ppt Ping Brennan

Similar presentations


Presentation on theme: "Introduction to Programming Java Lab 2: Variables and Number Types 1 JavaLab2 lecture slides.ppt Ping Brennan"— Presentation transcript:

1 Introduction to Programming Java Lab 2: Variables and Number Types 1 JavaLab2 lecture slides.ppt Ping Brennan (p.brennan@dcs.bbk.ac.uk)

2 Two Java Projects Project Name: JavaLab2a 2 Project Name: JavaLab2b Volume1 Purchase Price (fixed prices) Purchase Price using keyboard input (input from Keyboard) SwapCharacters

3 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

4 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

5 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."); }

6 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 ;

7 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 ;

8 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 main Scanner 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. */

9 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

10 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

11 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

12 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 HarryPotter 01234567891011 index

13 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 HarryPotter 01234567891011 index


Download ppt "Introduction to Programming Java Lab 2: Variables and Number Types 1 JavaLab2 lecture slides.ppt Ping Brennan"

Similar presentations


Ads by Google