Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC 111 Exam 3 Review Fall 2005.

Similar presentations


Presentation on theme: "CSC 111 Exam 3 Review Fall 2005."— Presentation transcript:

1 CSC 111 Exam 3 Review Fall 2005

2 Classes – Construction Behaviors
Here’s a class hierarchy A Vehicle has the following attributes: String manufacturer String make int year int numberOfWheels double engineSizeInLiters Vehicle Car Truck SportsCar

3 Classes – Construction Behaviors
Write a constructor for the Vehicle class that takes parameters that set all 5 attributes Write a constructor for Vehicle that takes all parameters except year. It should by default set year equal to the current year. Vehicle Car Truck SportsCar

4 Classes – Construction Behaviors
Vehicle Car Truck SportsCar

5 Classes – Construction Behaviors
A Car is a Vehicle which has the following constraints: Must have 4 wheels Has an additional double attribute, called trunkSize Write the fiver parameter constructor (manufacturer, make, year, engineSizeInLiters, trunkSize) for Car

6 Classes – Construction Behaviors
Write the fiver parameter constructor (manufacturer, make, year, engineSizeInLiters, trunkSize) for Car

7 Classes On the following slides, when I call the construction behavior
Car c = new Car(); I actually intend it to be that I’m sending in all 5 parameters – I’m running out of space so I left the parameters out. A real use of the Car constructor would look like: Car c = new Car(“Chevrolet”, “Camaro”, 1995, 5.7, 9.0);

8 Classes Given the hierarchy to the right, which of the following instructions are valid? Vehicle Vehicle v = new Vehicle(); Vehicle v = new Car(); Car c = new Vehicle(); Car c = new SportsCar(); Car c = new Truck(); Vehicle v = new Truck(); SportsCar sc = new SportsCar(); Truck t = new Truck(); Car Truck SportsCar

9 Classes Given the hierarchy to the right, which of the following instructions are valid? Vehicle Car Truck VALID Vehicle v = new Vehicle(); VALID Vehicle v = new Car(); INVALID Car c = new Vehicle(); VALID Car c = new SportsCar(); INVALID Car c = new Truck(); VALID Vehicle v = new Truck(); VALID SportsCar sc = new SportsCar(); VALID Truck t = new Truck(); SportsCar

10 Classes Vehicle Given the hierarchy and instructions to the right, what would be the answer to the following instance of questions? Car Truck SportsCar Vehicle v1 = new Vehicle(); Vehicle v2 = new Car(); Car c = new SportsCar(); Vehicle v3 = new Truck(); SportsCar sc = new SportsCar(); Is v1 an instanceof Car? Is v2 an instance of Car? Is v3 an instance of Car? Is c an instance of Car? Is sc an instance of Car? Is c an instance of SportsCar? List all classes v3 is an instanceof. List all variables that are List all classes v2 is an instanceof. an instanceof Car

11 Classes Vehicle Car Truck Given the hierarchy and instructions to the right, what would be the answer to the following instance of questions? SportsCar Vehicle v1 = new Vehicle(); Vehicle v2 = new Car(); Car c = new SportsCar(); Vehicle v3 = new Truck(); SportsCar sc = new SportsCar(); Is v1 an instanceof Car? FALSE Is v2 an instance of Car? TRUE Is v3 an instance of Car? FALSE Is c an instance of Car? TRUE Is sc an instance of Car? TRUE Is c an instance of SportsCar? TRUE List all classes v3 is an instanceof. Truck, Vehicle List all classes v2 is an instanceof. Car, Vehicle List all variables instanceof Car: v2, c, sc.

12 Classes Vehicle Assume all class definitions have get and set methods for the attributes they define (like getMake() in Vehicle, getTrunkSize() in Car …) Given the hierarchy and instructions to the right, write a public static void method called printManufacturer that could take any of the variables and print out their “manufacturer”. public static void printManufacturer(??) { ?? } Car Truck SportsCar Vehicle v1 = new Vehicle(); Vehicle v2 = new Car(); Car c = new SportsCar(); Vehicle v3 = new Truck(); SportsCar sc = new SportsCar();

13 Classes Vehicle Given the hierarchy and instructions to the right, write a public static void method that could take any of the variables and print out their “manufacturer”. Assume all class definitions have get and set methods for their attributes. public static void printManufacturer(Vehicle vehicle) { System.out.println( vehicle.getManufacturer()); } Car Truck SportsCar Vehicle v1 = new Vehicle(); Vehicle v2 = new Car(); Car c = new SportsCar(); Vehicle v3 = new Truck(); SportsCar sc = new SportsCar();

14 Classes Vehicle Assume that there is an abstract behavior called double computeFuelEfficiency() in the Vehicle class. Assume that method is implemented in Car and Truck. Which of the following statements are valid? Car Truck SportsCar Vehicle v = new Vehicle(); System.out.println(v.computeFuelEfficiency()); Car c = new Car(); System.out.println(c.computeFuelEfficiency()); Vehicle v2 = new Car(); System.out.println(v2.computeFuelEfficiency()); SportsCar sc = new SportsCar(); System.out.println(sc.computeFuelEfficiency());

15 Classes Vehicle v = new Vehicle();
System.out.println(v.computeFuelEfficiency()); ABOVE IS ILLEGAL – CAN’T CONSTRUCT VEHICLES (= new Vehicle()) BECAUSE ABSTRACT CLASS Car c = new Car(); System.out.println(c.computeFuelEfficiency()); TWO ABOVE ARE LEGAL Vehicle v2 = new Car(); System.out.println(v2.computeFuelEfficiency()); TWO ABOVE ARE LEGAL (You’re really a Car, Vehicles have it listed as callable method) SportsCar sc = new SportsCar(); System.out.println(sc.computeFuelEfficiency());

16 Classes Inheritance or dependency? Library and Book Student and Course
Politician and Governor Currency and Euro Ship and Aircraft Carrier Ship and Fleet

17 Classes Inheritance or dependency? Library and Book Dependency
Student and Course Dependency Politician and Governor Inheritance Currency and Euros Inheritance Ship and Aircraft Carrier Inheritance Ship and Fleet Dependency

18 File Input To the right is the start of a simple encryption program.
It already has a reverse static method which takes as a single parameter a String to reverse and that returns the reversed String, as well as code in main to ask the user what file to encrypt. Finish the program using file input instructions so that it 1) opens the requested file for reading, 2) reads and reverses each line in the file and prints the reversed lines to the screen, and 3) at the end closes the file.

19 File Input To the right is the start of a simple encryption program.
It already has a reverse static method which takes as a single parameter a String to reverse and that returns the reversed String, as well as code in main to ask the user what file to encrypt. Finish the program using file input instructions so that it 1) opens the requested file for reading, 2) reads and reverses each line in the file and prints the reversed lines to the screen, and 3) at the end closes the file.

20 File Output Test Program Output File

21 File Output A Student class has the following attributes:
String firstName String lastName String major int idNumber Write a saveToFile Behavior that Has no return type and a String parameter which is the filename to write to Opens the file specified by filename for writing Inside the file, write the data in this format lastName, firstName id major Make sure all items written are really written to the file and close the file using the appropriate behaviors

22 File Output A Student class has the following attributes:
String firstName String lastName String major int idNumber Write a saveToFileBehavior that Has no return type and a String parameter which is the filename to write to Open the file for writing Inside the file, write the data in this format lastName, firstName id major Make sure all items written are really written to the file and close the file using the appropriate behaviors

23 File Input/Output Review HourlyEmployee constructor for constructing objects from a file.

24 Arrays - Creating Write a Java instruction to create an array that could hold all 26 characters in the alphabet. Name the array whatever you would like. The primitive type for characters is char. Suppose you are creating an array to model a cash-register drawer. Each slot holds how many of each type of currency you have. There are eight types of currency you will hold in the drawer ($20, $10, $5, $1, quarters, nickels, dimes, and pennies). Remember that you can’t hold 1.5 quarters (they don’t break in half that easily!) Write a Java instruction to create an array for this problem. Name the array register. What would be the appropriate type for the array and how big would it need to be?

25 Arrays - Creating Write a Java instruction to create an array that could hold all lowercase characters in the alphabet. Name the array whatever you would like. The primitive type for characters is char. char[] alphabet = new char[26]; Suppose you are creating an array to model a cash-register drawer. Each slot holds how many of each type of currency you have. These are the types of currency you will hold in the drawer ($20 bills, $10 bills, $5 bills, $1 bills, quarters, nickels, dimes, and pennies). Remember that you can’t hold 1.5 quarters (they don’t break that easily!) Write a Java instruction to create an array for this problem. Name the array register. What would be the appropriate type for the array and how big would it need to be? int[] register = new int[8];

26 Arrays - Creating Suppose you are creating an array to model a cash-register drawer. Each slot holds how many of each type of currency you have. There are eight types of currency you will hold in the drawer ($20, $10, $5, $1, quarters, nickels, dimes, and pennies). Assume that the currency counts are held in the order listed above in your register array (register[0] is the number of 20’s, register[1] is the number of 10’s, … register[4] is the number of quarters, register[7] is the number of pennies). Write a (set of?) Java instruction(s) that indicates your register drawer starts with 5 each of 20, 10, 5, and 1 dollar bills and 10 each of quarters, nickels, dimes, and pennies.

27 Arrays – Creating/Writing
Suppose you are creating an array to model a cash-register drawer. Each slot holds how many of each type of currency you have. There are eight types of currency you will hold in the drawer ($20, $10, $5, $1, quarters, nickels, dimes, and pennies). Assume that the currency counts are held in the order listed above in your register array (register[0] is the number of 20’s, register[1] is the number of 10’s, register[4] is the number of quarters, register[7] is the number of pennies). Write a (set of?) Java instructions that indicates your register drawer starts with 5 each of 20, 10,5, and 1 dollar bills and 10 each of quarters, nickels, dimes, and pennies. int[] register = {5,5,5,5,10,10,10,10}; OR int[] register = new int[8]; register[0] = 5; register[1] = 5; register[2] = 5; register[3] = 5; register[4] = 10; register[5] = 10; register[6] = 10; register[7] = 10;

28 Arrays - Reading Write a set of instructions that computes how many total pieces of currency (how many coins and bills) are in the register? Your answer should be stored in the integer variable totalCurrencyCount.

29 Arrays – Reading - Answer
Write a set of instructions that computes how many total pieces of currency (how many coins and bills) are in the register? Your answer should be stored in the integer variable totalCurrencyCount.

30 Arrays – Reading What if you just wanted to count the number of coins in the drawer? Your answer should be stored in the integer variable totalCoinCount.

31 Arrays – Reading What if you just wanted to count the number of coins in the drawer? Your answer should be stored in the integer variable totalCoinCount.

32 Arrays Writing What would the array contents of the char array slogan2 be after the following array reading/writing instructions?

33 Arrays Writing What would the array contents be after the following array reading/writing instructions?


Download ppt "CSC 111 Exam 3 Review Fall 2005."

Similar presentations


Ads by Google