About me

3 Example Java Coding

tajaan secantikmanis on Sunday, July 22, 2012

Title: Object Oriented Programming
Topic: Class and objects

Question 1

Create a class Rectangle. The class has attributes length and width, each of which defaults to 1. It has methods that calculate the perimeter and the area of the rectangle. It has set and get methods (accessor and mutator) for both length and width. The set methods should verify that length and width are each floating-point numbers larger than 0.0 and less than 20.0. Write a program to test class Rectangle.


Question: 2

You operate several burger stalls distributed throughout Kampung Baru. Define a class named BurgerStall that has a member variable for the burger stall ID number and a member variable for how many burgers the stall has sold that day. Create a constructor that allows a user of the class to initialize both values.

Also create a method named justSold that increments the number of burgers the stall has sold by one. The idea is that this method will be invoked each time the stand sells a burger so that you can track the total number of burgers sold by the stall. Add another method that returns the number of burgers sold.

Finally, add a static variable that tracks the total number of burgers sold by all burger stalls and a static method that returns the value in this variable.

Write a main method to test your class with at least three burger stalls that each sell a variety of burgers.

Question 3

Create class SavingsAccount. Use a static variable annualInterestRate to store the annual interest rate for all account holders. Each object of the class contains a private instance variable savingsBalance indicating the amount the saver currently has on deposit. Provide method calculateMonthlyInterest to calculate the monthly interest by multiplying the savingsBalance by annualInterestRate divided by 12 - this interest should be added to savingsBalance.

Provide a static method modifyInterestRate that sets the annualInterestRate to a new value. Write a program to test class SavingsAccount. Instantiate two savingsAccount objects, saver1 and saver2, with balances of RM2000.00 and RM3000.00, respectively. Set annualInterestRate to 4%, then calculate the monthly interest and print the new balances for both savers. Then set the annualInterestRate to 5%, calculate the next month’s interest and print the new balances for both savers.


Please Share it! :)

{ 1 sweetie... read them below or add one }

cerita apakah said...

bila tengok soalan nih teringat jew time dulu-dulu duk belajar java nih hihi