Java Revision - WordPress.comDistinguish between a class and an object A class is the blueprint from...

43
Java Revision

Transcript of Java Revision - WordPress.comDistinguish between a class and an object A class is the blueprint from...

Page 1: Java Revision - WordPress.comDistinguish between a class and an object A class is the blueprint from which individual objects are created; And object is an instance of a class. How

Java Revision

Page 2: Java Revision - WordPress.comDistinguish between a class and an object A class is the blueprint from which individual objects are created; And object is an instance of a class. How

Distinguish between a class and an object

Page 3: Java Revision - WordPress.comDistinguish between a class and an object A class is the blueprint from which individual objects are created; And object is an instance of a class. How

Distinguish between a class and an object

◦ A class is the blueprint from which individual

objects are created;

◦ And object is an instance of a class.

Page 4: Java Revision - WordPress.comDistinguish between a class and an object A class is the blueprint from which individual objects are created; And object is an instance of a class. How

How would you declare a class called

‘Square’?

Page 5: Java Revision - WordPress.comDistinguish between a class and an object A class is the blueprint from which individual objects are created; And object is an instance of a class. How

How would you declare a class called

‘Square’?

◦ class Square {

◦ }

Page 6: Java Revision - WordPress.comDistinguish between a class and an object A class is the blueprint from which individual objects are created; And object is an instance of a class. How

How would you create an object, called

yellowSquare of a class called Square?

Page 7: Java Revision - WordPress.comDistinguish between a class and an object A class is the blueprint from which individual objects are created; And object is an instance of a class. How

How would you create an object, called

yellowSquare of a class called Square?

◦ Square yellowSquare = new Square();

Page 8: Java Revision - WordPress.comDistinguish between a class and an object A class is the blueprint from which individual objects are created; And object is an instance of a class. How

What is a Java method?

Page 9: Java Revision - WordPress.comDistinguish between a class and an object A class is the blueprint from which individual objects are created; And object is an instance of a class. How

What is a Java method?

◦ A method describes an action that can be carried

out on or by an object

Page 10: Java Revision - WordPress.comDistinguish between a class and an object A class is the blueprint from which individual objects are created; And object is an instance of a class. How

How would you call a method called

‘showSquare’ for an object called

yellowSquare?

Page 11: Java Revision - WordPress.comDistinguish between a class and an object A class is the blueprint from which individual objects are created; And object is an instance of a class. How

How would you call a method called

‘showSquare’ for an object called

yellowSquare?

◦ yellowSquare.showSquare();

Page 12: Java Revision - WordPress.comDistinguish between a class and an object A class is the blueprint from which individual objects are created; And object is an instance of a class. How

From which method does the execution

of a Java application start?

Page 13: Java Revision - WordPress.comDistinguish between a class and an object A class is the blueprint from which individual objects are created; And object is an instance of a class. How

From which method does the execution

of a Java application start?

◦ From method ‘main’

Page 14: Java Revision - WordPress.comDistinguish between a class and an object A class is the blueprint from which individual objects are created; And object is an instance of a class. How

Write down the main method declaration

Page 15: Java Revision - WordPress.comDistinguish between a class and an object A class is the blueprint from which individual objects are created; And object is an instance of a class. How

Write down the main method declaration

◦ public static void main (String[ ] args){

◦ }

Page 16: Java Revision - WordPress.comDistinguish between a class and an object A class is the blueprint from which individual objects are created; And object is an instance of a class. How

List three Java numeric variable types

Page 17: Java Revision - WordPress.comDistinguish between a class and an object A class is the blueprint from which individual objects are created; And object is an instance of a class. How

List three Java numeric variable types

◦ long

◦ short

◦ byte

◦ int

◦ float

◦ double

Page 18: Java Revision - WordPress.comDistinguish between a class and an object A class is the blueprint from which individual objects are created; And object is an instance of a class. How

Put the following Java primitive types in

order, smallest first:

◦ int

◦ long

◦ byte

◦ short

Page 19: Java Revision - WordPress.comDistinguish between a class and an object A class is the blueprint from which individual objects are created; And object is an instance of a class. How

Put the following Java primitive types in

order, smallest first:

◦ byte

◦ short

◦ int

◦ long

Page 20: Java Revision - WordPress.comDistinguish between a class and an object A class is the blueprint from which individual objects are created; And object is an instance of a class. How

How would you declare a Java variable

called length? (Where ‘length’ is the

length of a window in metres)

Page 21: Java Revision - WordPress.comDistinguish between a class and an object A class is the blueprint from which individual objects are created; And object is an instance of a class. How

How would you declare a Java variable

called length? (Where ‘length’ is the

length of a window in metres)

◦ float length;

Page 22: Java Revision - WordPress.comDistinguish between a class and an object A class is the blueprint from which individual objects are created; And object is an instance of a class. How

Declare a final variable called VAT whose

value is 0.18

Page 23: Java Revision - WordPress.comDistinguish between a class and an object A class is the blueprint from which individual objects are created; And object is an instance of a class. How

Declare a final variable called VAT whose

value is 0.18

◦ static final float VAT = 0.18;

Page 24: Java Revision - WordPress.comDistinguish between a class and an object A class is the blueprint from which individual objects are created; And object is an instance of a class. How

Explain the function of this line: System.out.printf ("%1.2f" , this.cost);

Page 25: Java Revision - WordPress.comDistinguish between a class and an object A class is the blueprint from which individual objects are created; And object is an instance of a class. How

Explain the function of this line: System.out.printf ("%1.2f" , this.cost);

◦ It outputs this.cost to two decimal places

Page 26: Java Revision - WordPress.comDistinguish between a class and an object A class is the blueprint from which individual objects are created; And object is an instance of a class. How

Write a line that outputs ‘The total is’ and

the value of instance variable total on the

screen.

Page 27: Java Revision - WordPress.comDistinguish between a class and an object A class is the blueprint from which individual objects are created; And object is an instance of a class. How

Write a line that outputs ‘The total is’ and

the value of instance variable total on the

screen.

◦ System.out.println(“The total is: “ + this.total);

Page 28: Java Revision - WordPress.comDistinguish between a class and an object A class is the blueprint from which individual objects are created; And object is an instance of a class. How

Write a method called outputTriangle

that outputs the values of sides of a

triangle object a, b and c.

Page 29: Java Revision - WordPress.comDistinguish between a class and an object A class is the blueprint from which individual objects are created; And object is an instance of a class. How

Write a method called outputTriangle

that outputs the values of sides of a

triangle object a, b and c.

◦ public void outputTriangle(){

System.out.println (“Side a:” + this.a);

System.out.println (“Side b:” + this.b);

System.out.println (“Side c:” + this.c);

◦ }

Page 30: Java Revision - WordPress.comDistinguish between a class and an object A class is the blueprint from which individual objects are created; And object is an instance of a class. How

Write a method called getVATCost that

calculates and returns the cost (VAT

included) of an object passed to it

(Assume VAT rate is stored in a global

constant variable called VAT)

Page 31: Java Revision - WordPress.comDistinguish between a class and an object A class is the blueprint from which individual objects are created; And object is an instance of a class. How

Write a method called getVATCost that

returns the cost (VAT included) of an

object (chair) passed to it

(Assume VAT rate is stored in a global

constant variable called VAT)

public float getVATCost(){

◦ float VATCost = (this.cost* VAT) + this.cost;

◦ return VATCost;

}

Page 32: Java Revision - WordPress.comDistinguish between a class and an object A class is the blueprint from which individual objects are created; And object is an instance of a class. How

Write a simple Java method called

findArea that finds and outputs the area

of a rectangle object of sides a and b

Page 33: Java Revision - WordPress.comDistinguish between a class and an object A class is the blueprint from which individual objects are created; And object is an instance of a class. How

Write a simple Java method called

findArea that finds and outputs the area

of a rectangle object of sides a and b

◦ public void findArea() {

float area = this.a * this.b;

System.out.println (“Area : “ + this.area + “sq cm”);

◦ }

Page 34: Java Revision - WordPress.comDistinguish between a class and an object A class is the blueprint from which individual objects are created; And object is an instance of a class. How

Implement a case/switch structure in a method (call your method makeMenuChoice) that outputs the following Menu and then writes on the screen the name of the Menu Option chosen.

◦ Main Menu

◦ 1. Enter Student Details

◦ 2. View Classes

◦ 3. View Marks

◦ 4. View Prize Winners

◦ 5. Exit

Page 35: Java Revision - WordPress.comDistinguish between a class and an object A class is the blueprint from which individual objects are created; And object is an instance of a class. How

Implement a case/switch structure in a method (call your method makeMenuChoice) that outputs the following Menu and then writes on the screen the name of the Menu Option chosen. ◦ Main Menu

◦ 1. Enter Student Details

◦ 2. View Classes

◦ 3. View Marks

◦ 4. View Prize Winners

◦ 5. Exit

System.out.println (“Main Menu”);

System.out.println (“1. Enter Student Details”);

System.out.println (“2. View Classes”);

System.out.println (“3. View Marks”);

System.out.println (“4. View Prize Winners”);

System.out.println (“5. Exit”);

byte choice = input.nextByte();

switch (choice) {

case 1: {

System.out.println (“Enter Student Details”);

break;

}

case 2: [ etc ]

}

Page 36: Java Revision - WordPress.comDistinguish between a class and an object A class is the blueprint from which individual objects are created; And object is an instance of a class. How

Name the three types of Java looping

structures

Page 37: Java Revision - WordPress.comDistinguish between a class and an object A class is the blueprint from which individual objects are created; And object is an instance of a class. How

Name the three types of Java looping

structures

◦ for

◦ while

◦ do..while

Page 38: Java Revision - WordPress.comDistinguish between a class and an object A class is the blueprint from which individual objects are created; And object is an instance of a class. How

Answer True or False

◦ A For Loop is a predetermined loop

◦ A do..while loop will loop 0 or more times

◦ A while..do loop will loop one or more times

Page 39: Java Revision - WordPress.comDistinguish between a class and an object A class is the blueprint from which individual objects are created; And object is an instance of a class. How

Answer True or False

◦ A For Loop is a predetermined loop True

◦ A do..while loop will loop 0 or more times False

◦ A while..do loop will loop one or more times

False

Page 40: Java Revision - WordPress.comDistinguish between a class and an object A class is the blueprint from which individual objects are created; And object is an instance of a class. How

Write a method called handleTest that reads 10 marks into an array called markList.

Write a method called findHighest that finds and outputs the highest mark in array markList

Write a method called findLowest that finds and outputs the lowest mark in array markList

Write a method called findAverage that finds and outputs the average mark in array markList

Page 41: Java Revision - WordPress.comDistinguish between a class and an object A class is the blueprint from which individual objects are created; And object is an instance of a class. How

Write a method called handleTest that reads 10 marks into an array called markList.

public void handleTest() {

int[] markList = new int[10]

for (i = 0; i<10; i++){

System.out.println (‘Enter mark’);

this.markList[i] = input.nextInt();

}

}

Page 42: Java Revision - WordPress.comDistinguish between a class and an object A class is the blueprint from which individual objects are created; And object is an instance of a class. How

Write a method called findHighest that finds and outputs the highest mark in array markList

public void findHighest() {

int i;

int highest = 0;

for (i = 0; i <10; i++) {

if (markList[i] > highest){

highest = markList[i];

}

System.out.println (“The Highest Mark is “ + highest);

}

}

Page 43: Java Revision - WordPress.comDistinguish between a class and an object A class is the blueprint from which individual objects are created; And object is an instance of a class. How