Java Revision
Distinguish between a class and an object
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.
How would you declare a class called
‘Square’?
How would you declare a class called
‘Square’?
◦ class Square {
◦ }
How would you create an object, called
yellowSquare of a class called Square?
How would you create an object, called
yellowSquare of a class called Square?
◦ Square yellowSquare = new Square();
What is a Java method?
What is a Java method?
◦ A method describes an action that can be carried
out on or by an object
How would you call a method called
‘showSquare’ for an object called
yellowSquare?
How would you call a method called
‘showSquare’ for an object called
yellowSquare?
◦ yellowSquare.showSquare();
From which method does the execution
of a Java application start?
From which method does the execution
of a Java application start?
◦ From method ‘main’
Write down the main method declaration
Write down the main method declaration
◦ public static void main (String[ ] args){
◦ }
List three Java numeric variable types
List three Java numeric variable types
◦ long
◦ short
◦ byte
◦ int
◦ float
◦ double
Put the following Java primitive types in
order, smallest first:
◦ int
◦ long
◦ byte
◦ short
Put the following Java primitive types in
order, smallest first:
◦ byte
◦ short
◦ int
◦ long
How would you declare a Java variable
called length? (Where ‘length’ is the
length of a window in metres)
How would you declare a Java variable
called length? (Where ‘length’ is the
length of a window in metres)
◦ float length;
Declare a final variable called VAT whose
value is 0.18
Declare a final variable called VAT whose
value is 0.18
◦ static final float VAT = 0.18;
Explain the function of this line: System.out.printf ("%1.2f" , this.cost);
Explain the function of this line: System.out.printf ("%1.2f" , this.cost);
◦ It outputs this.cost to two decimal places
Write a line that outputs ‘The total is’ and
the value of instance variable total on the
screen.
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);
Write a method called outputTriangle
that outputs the values of sides of a
triangle object a, b and c.
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);
◦ }
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)
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;
}
Write a simple Java method called
findArea that finds and outputs the area
of a rectangle object of sides a and b
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”);
◦ }
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
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 ]
}
Name the three types of Java looping
structures
Name the three types of Java looping
structures
◦ for
◦ while
◦ do..while
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
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
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
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();
}
}
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);
}
}