Robotics C++ Physics II AP Physics B Electronics Java Astronomy Other Courses Summer Session  

 Exercises

 

Exercise 1

Exercise 2

Exercise 3

Exercise 4

Exercise 5

Exercise 6

 

 

Exercise 1

 

Assume that you are the manager of a restaurant.

 

Create a class called RestaurantProgram. Include

1.    instance variables for the price for cokes, fries, and burgers and set a tax rate. Make them private instance variables.

2.    a parameterized constructor that will set the values of the variables in part 1 as passed by the main method described below.

3.    additional instance variables as required (numCokes, for example)

4.    a method to greet the customer (assume only 1 customer - do not use a loop)

5.    a method to state prices

6.    a method to take number of cokes and assign this number to the appropriate instance variable in part 3 above.

7.    a method to take number of fries and assign this number to the appropriate instance variable in part 3 above.

8.    a method to take number of burgers and assign this number to the appropriate instance variable in part 3 above.

9.    a method to calculate the total price

10.  a method to announce the total price

Create, in the same project as the above class, a class called RestaurantDriver that will

 

1.   Create an object of the RestaurantProgram class and pass values for the instance variables described in item 1 above.

 

2.   Use the object to call methods described in parts 4 through 8 above.

 

3.  Use the object to call the methods in parts 9 and 10 above.

 

 

NOTES

 

Can sell any 3 legitimate items

Add the optional part for 2 additional points (102 max grade) - Constructor calls 4 separate data validation methods to check the sanity of the tax

rate and the 3 prices.

 

Can prompt user for order in the main and pass to the other class via a constructor or handle this in the Restaurant class. All methods other than the main must be in the Restaurant class

 

 

Exercise 2

 

Create a person class with the followng instance variables: number of eyes, number of noses, and number of brains.

Create a default constructor that will assign 1 to each of the instance variables.

Create a parameterized constuctor that will accept user input to assign values.

Create a driver class that

    creates 3 objects of the person class, each with different parameters, using the parameterize constructor.

    creates a one dimensional array and stores the above 3 objects in it.

    prints the contents of the array 

 

Exercise 3

 

Intro

 

2.   The specifications of a class that models the fuel efficiency of a car could be:

 

Variables

int myStartMiles;        // Starting odometer reading

int myEndMiles;          // Ending odometer reading

double myGallonsUsed;    // Gallons of gas used between the readings

 

Constructors

// Creates a new instance of a Car object with the starting odometer reading.

Car(int odometerReading)                                         

 

Methods

//Simulates filling up the tank. Record the current odometer reading and the number of gallons to fill the tank

void fillUp(int odometerReading, double gallons)

 

// Calculates and returns the miles per gallon for the car.

double calculateMPG()  

 

Requirement

 

1.   Implement a Car class with the following properties.

 

       a.   A Car keeps track of the start odometer reading, ending odometer reading, and the number of gallons used between readings.

       b.   The initial odometer reading is specified in the constructor

       c.   A method calculateMPG calculates and returns the mile per gallon for the car..

       d.   A method fillup simulates filling up the tank at a gas station: odometerReading is the current odometer reading and gallons is the number

             of gallons that filled the tank. Save these values in instance variables.

       e.   With this information, miles per gallon can be calculated. Write the method so that it updates the instance variables each time it is called

            (simulating another visit to the pumps). After each call, calculateMPG will calculate the latest miles per gallon.

2.   Write a testing class with a main method that constructs a car and calls fillUp and calculateMPG a few times.  Sample usage would be

 

Car auto = new Car(15);  // initial odometer reading of 15 miles

auto.fillUp(250, 10);          // odometer is at 250 miles, fillup with 10 gallons of gas

                                          // repeat auto.fillup line for additional fillups

                                                           

System.out.println(auto.calculateMPG())   // print miles per gallon

 

3.   Write a testing class with a main method that constructs a car and calls fillUp and calculateMPG a few times.  A sample run of the program would give (values in bold italics represent input from the user):

 

New car odometer reading: 15

 

Filling Station Visit

  odometer reading: 250

  gallons to fill tank: 10

 

Miles per gallon: 23.50

 

Filling Station Visit

  odometer reading: 455

  gallons to fill tank: 12.5

 

Miles per gallon: 16.40

 

 

 

Exercise 4

 

 

 

Exercise 5

 

 

 

Exercise 6