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

13
Introduction to Programming Java Lab 2: Variables and Number Types 1 JavaLab2 lecture slides.ppt Ping Brennan ([email protected])

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

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

Introduction to Programming

Java Lab 2:Variables and Number Types

1JavaLab2 lecture slides.ppt

Ping Brennan ([email protected])

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

Two Java Projects

Project Name: JavaLab2a

2

Project Name: JavaLab2b

Volume1

Purchase Price(fixed prices)

Purchase Price using keyboard

input(input from Keyboard)

SwapCharacters

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

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

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

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

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

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

}

}

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

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;

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

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;

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

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. */

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

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

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

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

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

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

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

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

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

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