Results 1 to 5 of 5
Thread: Exam prep Question
- 05-09-2011, 05:16 PM #1
Member
- Join Date
- May 2011
- Posts
- 1
- Rep Power
- 0
Exam prep Question
Hi All
I have a Java exam on Friday and am going through some past papers to try and brush up. This is a written exam but Im trying to write some of the code questions in eclipse for practise.
Does this code look alright? it compiles but im not sure it reaches the requirements of the question.
Thanks in advance
Question
Write a rent() method in the game class, which will rent out a game. The cost of renting the game is €5.00 per night, the number of nights is to be passed in an as an argument to the method, the maximum number of nights a game can be rented for is 5, if number of nights greater than this then an error message is printed to the user. if the game is rented for 3 or more night the cost is €15.00. The method should check there is a copy of the game available for rent and should decrement the appropriate variable to indicate that the game is being is being rented. The totalEarned attribure should be updated appropriately.
Java Code:public class game { private String name; private String Genre; private int noOfCopies; private double totalEarned; private int noOut; public game(String name, String Genre, int noOfCopies) { this.name = name; this.Genre = Genre; if(noOfCopies <= 0) { System.out.println("Copies must be more than 0!"); totalEarned = 0; noOut = 0; } this.noOfCopies = noOfCopies; } public void rent(int numNights) { double cost = 5.00; if(numNights > 5) { System.out.println("Number of nights must be less than5!"); } if(numNights >= 3 || numNights <= 5) { cost = 15.00; { if(noOfCopies > 0) { noOfCopies --; } totalEarned = cost; } } } }
- 05-09-2011, 05:22 PM #2
I can see plenty of problems with it, actually. First of all, you only set totalEarned and noOut to 0 if 0 or less copies exist? Second, you never print an error if there are no copies available to rent. Third, you never increment totalEarned, you only set it to cost. Those are the errors that jumped out at me right away; there might be others.
- 05-09-2011, 07:27 PM #3
Senior Member
- Join Date
- Mar 2011
- Posts
- 261
- Rep Power
- 3
Why don't you test it yourself to see if it meets the requirements listed by instructor? If you run into any problems then post the error code here and the line that causes it.
- 05-10-2011, 01:01 AM #4
I am new but just suggesting, correct if wrong but I think you would need a while loop for days input to prompt the user to input the correct one. from what I saw it would just print that line and have to have user start program over
- 05-10-2011, 01:11 AM #5
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Similar Threads
-
Sun Exam Vouchers
By dtkmn in forum Java CertificationReplies: 2Last Post: 04-28-2010, 12:31 PM -
Java 6 Exam
By varsh in forum Advanced JavaReplies: 3Last Post: 04-23-2010, 06:14 PM -
Preparing for Exam
By AWPtic in forum New To JavaReplies: 4Last Post: 04-13-2009, 02:34 PM -
SCJP exam dum
By appoint_raj in forum Java CertificationReplies: 1Last Post: 01-29-2009, 09:29 AM -
please help with exam problems
By Nari in forum New To JavaReplies: 4Last Post: 04-21-2008, 08:47 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks