Results 1 to 5 of 5
  1. #1
    tonto2010 is offline Member
    Join Date
    May 2011
    Posts
    1
    Rep Power
    0

    Default 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;
    			}
    
    		}
    	}
    	}

  2. #2
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    384
    Rep Power
    4

    Default

    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.

  3. #3
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    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.

  4. #4
    dpspine420's Avatar
    dpspine420 is offline Member
    Join Date
    May 2011
    Posts
    9
    Rep Power
    0

    Default

    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

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Quote Originally Posted by dpspine420 View Post
    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
    While it makes sense to re-prompt, the assignment doesn't say to re-prompt.

    @op: Your condition is if(arg >= 3 || arg <= 5) is incorrect. What if the user enters 2? Will the condition produce true or false?

Similar Threads

  1. Sun Exam Vouchers
    By dtkmn in forum Java Certification
    Replies: 2
    Last Post: 04-28-2010, 12:31 PM
  2. Java 6 Exam
    By varsh in forum Advanced Java
    Replies: 3
    Last Post: 04-23-2010, 06:14 PM
  3. Preparing for Exam
    By AWPtic in forum New To Java
    Replies: 4
    Last Post: 04-13-2009, 02:34 PM
  4. SCJP exam dum
    By appoint_raj in forum Java Certification
    Replies: 1
    Last Post: 01-29-2009, 09:29 AM
  5. please help with exam problems
    By Nari in forum New To Java
    Replies: 4
    Last Post: 04-21-2008, 08:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •