![]() |
![]() Second Summer 2002 |
Try to solve these problems to practice some of the things you learned
this week. In the examples that follow, your program's answers are always
in blue
, to distinguish them from
what you would type as a user. Remember: the resulting programs are
elementary, and the problems are interesting.
1. |
Write a program that asks for an initial balance amount. Create a
BankAccount object with that amount. Then ask for a deposit
amount and a withdrawal amount. Carry out the deposit and withdrawal, then
print the remaining balance. Use ConsoleReader from Lab Notes
Two, and please place your main method in the class
BankAccount . |
Here's a sample run of such a program:
frilled.cs.indiana.edu%javac BankAccount.java frilled.cs.indiana.edu%java BankAccount Hello, and welcome to JavaOne Bank. An account will be created for you. What will the initial balance be? Type it now: -40.2 The current balance in your account is: -40.2 You now want to make a deposit. How much? Type the amount here: 120.3 The current balance in your account is: 80.1 You now want to make a withdrawal. How much? Type it now: 34 The current balance in your account is: 46.099999999999994 Thanks for using class BankAccount. Good-bye! frilled.cs.indiana.edu% |
2. |
Implement a class Employee . An employee has a name (a String )
and a salary (a double ). Write a default constructor, a constructor with two
parameters (name and salary), and methods to return the name and salary. Write a small
program to test your class. |
Here's a sample run of such a program:
frilled.cs.indiana.edu%java Employee Creating a new employee. Please type the name: Larry Bird Please specify the salary: 200000 New employee has been created. Name of employee: Larry Bird Salary: 200000.0 Thank you for testing class Employee. frilled.cs.indiana.edu% |
3. |
Implement a class Employee . An employee has a name (a String ) and
a salary (a double ). Write a default constructor, a constructor with
two parameters (name and salary), and methods to return the name and salary. Test
your program, then enhance the class by adding a method
that raises the employee's salary by a certain percentage. |
Here's a sample run of such a program:
frilled.cs.indiana.edu%java Employee Creating a new employee. Please type the name: Michael Jordan Please specify the salary: 300000 New employee has been created. Name of employee: Michael Jordan Salary: 300000.0 Raising the salary of Michael Jordan By what percentage (e.g., 10, 20, etc.)? 10.5 Name of employee: Michael Jordan Current salary: 331500.0 Thank you for testing class Employee. frilled.cs.indiana.edu% |
4. |
Implement a class Car with the following properties. A car has a certain fuel
efficiency (measured in miles per gallon or liters per km -- pick one) and a certain amount
of fuel in the gas tank. The efficiency is specified in the constructor,and the initial fuel
level is 0. Supply a method drive that simulates driving the car for a certain
distance, reducing the fuel level in the gas tank, and methods getFuelLevel ,
returning the current fuel level, and tank , to tank up. |
Sample usage of the class:
Should produce:public static void main(String[] args) { Car myBeemer = new Car(29); System.out.println(myBeemer.getFuelLevel()); myBeemer.tank(20); System.out.println(myBeemer.getFuelLevel()); myBeemer.drive(100); System.out.println(myBeemer.getFuelLevel()); } frilled.cs.indiana.edu%java Car 0.0 20.0 16.551724137931036 |
5. |
Change the purse program Coins6 (page 123) to ask the user to supply coins
in a different currency. For example, you can use the following collection of German coins:
What changes did you have to make? What changes would you have to make to thenew Coin(0.01, "Pfennig"); new Coin(0.1, "Groschen"); new Coin(1.0, "Mark"); Coins4
(page 82) program to change the currency? Which is easier? (Answer these questions in a comment that
you can include either at the top or at the bottom of the program you submit).
|
Here's a sample run of such a program:
frilled.cs.indiana.edujava Coins6 Hello and welcome to Coins6 program. Please specify four types of coins. Anything goes. Name of coin1 (e.g. groschen, mark, pfennig etc.): pfennig Value of pfennig (i.e., 0.01, 0.10, 0.25, etc.): 0.01 Coin pfennig (0.01) created. ------------------------------------ Name of coin2: groschen Value of groschen: 0.1 Coin groschen (0.1) created. ------------------------------------ Name of coin3: mark Value of mark: 1 Coin mark (1.0) created. ------------------------------------ Name of coin4: germanNickel Value of germanNickel: 0.05 Coin germanNickel (0.05) created. ------------------------------------ How many pfennig do you have? 2 How many groschen do you have? 3 How many mark do you have? 1 How many germanNickel do you have? 4 The total value is: 1.52 frilled.cs.indiana.edu |
6. |
Add a method
to the Purse class that asks the user how many coins of that type
to add to the purse and that updates the coin count. Then change Coins6
(page 123) and test your new method.
|
Here's a sample run of such a program:
frilled.cs.indiana.edu%java Coins6 Hello and welcome to Coins6 program. How many pennies can you donate? 3 How many dimes can you donate? 4 How many quarters can you donate? 5 How many dollars can you donate? 6 The total value is: 7.68 frilled.cs.indiana.edu% |
7. |
Implement a class Student . For the purpose of this
exercise, a student has
To compute the latter, you also need to store the number of quizzes that the student took. |
Here's a sample run of such a program:
Should produce:public static void main(String[] args) { Student a = new Student("Larry"); a.addQuiz(10); a.addQuiz(9); a.addQuiz(8); System.out.println("Grade report for: " + a.getName()); System.out.println("Total score: " + a.getTotalScore()); System.out.println("Average score: " + a.getAverageScore()); } frilled.cs.indiana.edu%java Student Grade report for: Larry Total score: 27.0 Average score: 9.0 frilled.cs.indiana.edu% |
8. |
Implement a class Product . A product has
Supply methodsnew Product("Toaster", 29.95)
|
Here's a sample run of such a program:
Should produce:public static void main(String[] args) { Product a = new Product("Toaster", 29.95); Product b = new Product("Phone", 35.55); a.printProduct(); a.setPrice(a.getPrice() - 5.00); a.printProduct(); b.printProduct(); b.setPrice(b.getPrice() - 5.00); b.printProduct(); } frilled.cs.indiana.edu%java Product Product name: Toaster Product price: 29.95 ------------------------------ Product name: Toaster Product price: 24.95 ------------------------------ Product name: Phone Product price: 35.55 ------------------------------ Product name: Phone Product price: 30.549999999999997 ------------------------------ frilled.cs.indiana.edu% |
9. |
Implement a class Circle that has methods
|
Here's a sample run of such a program:
Should produce:public static void main(String[] args) { ConsoleReader c = new ConsoleReader(System.in); System.out.println("Please specify the radius of your circle:"); Circle a = new Circle(c.readDouble()); System.out.println("Circle created. "); System.out.println("Area: " + a.getArea()); System.out.println("Circumference: " + a.getCircumference()); System.out.println("Good-bye!"); } frilled.cs.indiana.edu%java Circle Please specify the radius of your circle: 1.0 Circle created. Area: 3.141592653589793 Circumference: 6.283185307179586 Good-bye! frilled.cs.indiana.edu%java Circle Please specify the radius of your circle: 3.14 Circle created. Area: 30.974846927333928 Circumference: 19.729201864543903 Good-bye! frilled.cs.indiana.edu% |
10. |
Implement a class BeerCan with methods
|
Here's a sample run of such a program:
Could produce:public static void main(String[] args) { ConsoleReader c = new ConsoleReader(System.in); System.out.println("Please specify the height of the BeerCan."); double height = c.readDouble(); System.out.println("Please specify the radius of the BeerCan."); double radius = c.readDouble(); BeerCan b = new BeerCan(height, radius); System.out.println("The BeerCan has been created."); System.out.println("Its surface area is: " + b.getSurfaceArea()); System.out.println("Its volume is: " + b.getVolume()); } frilled.cs.indiana.edu%java BeerCan Please specify the height of the BeerCan. 2.5 Please specify the radius of the BeerCan. 1.0 The BeerCan has been created. Its surface area is: 15.707963267948966 Its volume is: 7.853981633974483 frilled.cs.indiana.edu%java BeerCan Please specify the height of the BeerCan. 2 Please specify the radius of the BeerCan. 1 The BeerCan has been created. Its surface area is: 12.566370614359172 Its volume is: 6.283185307179586 frilled.cs.indiana.edu% |
Good luck and don't forget: the harder they seem the more you can learn from them.
But these problems are not difficult. They're just that: good practice.