3 Replies - 1447 Views - Last Post: 06 October 2012 - 05:30 PM Rate Topic: -----

#1 jacobTheMechanic  Icon User is offline

  • D.I.C Head

Reputation: 14
  • View blog
  • Posts: 89
  • Joined: 18-September 12

class and method creation in java

Posted 06 October 2012 - 12:14 PM

Here is the problem that I need to answer...

Write a class named GasTank containing:
An instance variable named amount of type double, initialized to 0.
A method named addGas that accepts a parameter of type double . The value of the amount instance variable is increased by the value of the parameter.
A method named useGas that accepts a parameter of type double . The value of the amount instance variable is decreased by the value of the parameter. However, if the value of amount is decreased below 0 , amount is set to 0 .
A method named isEmpty that accepts no parameters. isEmpty returns a boolean value: true if the value of amount is less than 0.1 , and false otherwise.
A method named getGasLevel that accepts no parameters. getGasLevel returns the value of the amount instance variable.


Here is my code...


public class GasTank
{
	private double amount = 0;
	public void addGas(double n1)
	{
		amount = amount + n1;
	}
	public void useGas(double n2)
	{
		if ((amount - n2) > 0 )
		{
			amount = amount - n2;
		}
		else
		{ 
			amount = 0;
		}
		
	}
	public boolean isEmpty()
	{
		if (amount < 0.1)
		{
			isEmpty = true;
		}
		
	}
	public double getGasLevel()
	{
			return amount;
	}
		
}


It keeps asking "are you sure you want to use n1 and n2?" I don't understand why this isn't acceptable

This post has been edited by macosxnerd101: 06 October 2012 - 01:21 PM
Reason for edit:: Please use code tags


Is This A Good Question/Topic? 0
  • +

Replies To: class and method creation in java

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10447
  • View blog
  • Posts: 38,690
  • Joined: 27-December 08

Re: class and method creation in java

Posted 06 October 2012 - 01:24 PM

Based on the code you have provided, I don't see why that prompt is printed out. Nor do I understand why this is a problem.
Was This Post Helpful? 0
  • +
  • -

#3 jacobTheMechanic  Icon User is offline

  • D.I.C Head

Reputation: 14
  • View blog
  • Posts: 89
  • Joined: 18-September 12

Re: class and method creation in java

Posted 06 October 2012 - 03:45 PM

So do you think that would work? The output from the myProgrammingLab (the environment where our coursework is completed) is so ambiguous that it provides little help, making seemingly simple problems take > 30 minutes to complete.
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10447
  • View blog
  • Posts: 38,690
  • Joined: 27-December 08

Re: class and method creation in java

Posted 06 October 2012 - 05:30 PM

Do I think your code will be accepted by the automated grading tool? I don't know, as I haven't thoroughly reviewed and tested it against the requirements. Your instructions should be thorough enough for you to make an attempt to do so.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1