Java

TOPIC 29 – FUNCTIONS & OBJECTS

 

LESSON WORK

 

 

QUESTION 1 – MIDDLE OF RECTANGLE


Write a function that gets a rectangle object as parameter and returns the middle point of the rectangle object.   This will require that students create a Rectangle class and a Point class.

 

 

QUESTION 2 – SCALED CIRCLE

 

Write a function that gets a Circle object as parameter as well and a double variable called scale.  The functions returns a new circle with the new scale.  Students will have to create a Circle class before starting.

 

QUESTION 3 – GLASSES

 

Consider the following Glass class that represents a normal cylindrical glass that you can drink out of. 

public class Glass

{

    public double radius;

    public double height;

    public String contents;

    public double percentFull;  //0.50 = 50% full

}                               //1.00 = 100% full


PART 1

In the main function of class called Tester, create two different glass objects.  The first, named glass1 has a radium of 4cm and a height of 12cm.  It contains “water” and is 80% full.  The second glass object is named glass2 and has a radius of 3cm and a height 16cm.  It also contains “water” and is 90% full.

 

PART 2

Still in the class Tester, write a function called volumeOfGlass that gets a glass object and returns the total volume for that glass.  Test your function.

PART 3
Again in the class Tester, write a function called volumeOfLiquid that gets a glass object and returns the total volume of the liquid in that glass.  Test your function.

PART 4

Again in the class Tester, write a function called remainingRoom that gets a glass object and returns the unused volume left in the glass.  Note that you might want to use your previously created functions to help you.  Test your function.

 

PART 5

Back in the main function, create a third glass.  Its name is dglass which is short for destination glass.  It has a radius of 6cm and the height 18cm.  It is empty.

 

PART 6
In the class Tester, write a function called enoughRoom() that gets three glass objects.  It determines if there is enough room in the first glass to empty the other glasses into it without spilling over.  The function simply returns true if there is room and false if there isn’t enough room.  Test your function.  Note that you might want to use some of your previous functions here.