473,320 Members | 2,027 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

How do you turn a psuedocode into an appropriate code for java to understand?

i do not understand how to return a range method from this code i made. i have the psuedocode for it, but i do not know how to turn it into a legit code. Also, can someone double check my other methods as well?

Expand|Select|Wrap|Line Numbers
  1. /**
  2.  * Class to determine Car's range of miles. 
  3.  * 
  4.  * @author Kelvynn Cayanan 
  5.  * @version 2/2/2014
  6.  */
  7. public class Car
  8. {
  9.     // instance variables - 
  10.     private double miles;
  11.     private double gallons;
  12.     private double gas;
  13.  
  14.     /**
  15.      * Constructor for objects of class Car
  16.      */
  17.     public Car()
  18.     {
  19.  
  20.     }
  21.     public Car(double initialGas)
  22.     {
  23.         gas = initialGas;
  24.     }
  25.  
  26.     public void addGas(double gas)
  27.     {
  28.         // Increases amount of gas in gas tank.
  29.         gallons = gallons + gas;
  30.     }
  31.  
  32.     public void drive(double drive)
  33.     {
  34.         // Decreases amount of gas in gas tank.
  35.         double newdrive = (drive/miles) - gas;
  36.         drive = newdrive;
  37.     }
  38.  
  39.     public double range(double range)
  40.     {
  41.         //**calculates range, the number of miles the car can travel until the gas tank is empty */
  42.         double newrange = miles * gas;
  43.         range = newrange;
  44.         return range;
  45.     }
  46. }
  47.  
  48.  
  49. here is a class that i am supposed to implement with the class i made above.
  50.  
  51. /**
  52.  * Uses Cars.
  53.  * 
  54.  * @author Anthony W. Smith 
  55.  * @version 6/15/2009
  56.  */
  57. public class CarUser
  58. {
  59.     /**
  60.      * Constructor for objects of class CarUser
  61.      */
  62.     public CarUser()
  63.     {
  64.         Car honda = new Car(30.0);      // 30 miles per gallon
  65.  
  66.         honda.addGas(9.0);              // add 9 more gallons
  67.         honda.drive(210.0);             // drive 210 miles
  68.  
  69.         // print range remaining
  70.         System.out.println("Honda range remaining: " + honda.range());
  71.  
  72.         Car toyota = new Car(26.0);      // 26 miles per gallon
  73.  
  74.         toyota.addGas(4.5);              // add 4.5 more gallons
  75.         toyota.drive(150.0);             // drive 150 miles
  76.  
  77.         // print range remaining
  78.         System.out.println("Toyota range remaining: " + toyota.range());
  79.     }
  80. }
Feb 3 '14 #1
2 1457
my first class comes out clean with no syntax errors, but when i try to compile the second class, it says
" method range in class Car cannot be applied to given types;
required: double; found: no argument: reason: actual and formal argument lists differ in length
Feb 3 '14 #2
Nepomuk
3,112 Expert 2GB
Hi k3lvynn and welcome to bytes.com!

In your Car class you have the following function:
Expand|Select|Wrap|Line Numbers
  1. public double range(double range)
  2. {
  3.     //**calculates range, the number of miles the car can travel until the gas tank is empty */
  4.     double newrange = miles * gas;
  5.     range = newrange;
  6.     return range;
  7. }
and in your CarUser class you try to call it like this:
Expand|Select|Wrap|Line Numbers
  1. // print range remaining
  2. System.out.println("Honda range remaining: " + honda.range());
So, you're trying to call the function without arguments while you defined it with an argument. From what your function definition looks like you don't really need a value to be passed though as you never read it; so you probably want to change your function definition to this:
Expand|Select|Wrap|Line Numbers
  1. public double range()
  2. {
  3.     //**calculates range, the number of miles the car can travel until the gas tank is empty */
  4.     double newrange = miles * gas;
  5.     double range = newrange;
  6.     return range;
  7. }
or alternatively (and shorter) this:
Expand|Select|Wrap|Line Numbers
  1. public double range()
  2. {
  3.     //**calculates range, the number of miles the car can travel until the gas tank is empty */
  4.     double newrange = miles * gas;
  5.     return newrange;
  6. }
or even this:
Expand|Select|Wrap|Line Numbers
  1. public double range()
  2. {
  3.     //**calculates range, the number of miles the car can travel until the gas tank is empty */
  4.     return miles * gas;
  5. }
If your function signature (public double range(double) in this case) has values between the braces that means you'll be handing input into the function; something you don't have to here. Also, you couldn't change the value of range within the function as Java has a mixture of pass-by-reference and pass-by-value which stops you from doing stuff like that.
Feb 3 '14 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Martin | last post by:
Hi All, I am a relative newbie to Java / Applets, but am despirately after some help ! I have got the following code, which is basically a listing with button items along the sides, allowing...
13
by: cjl | last post by:
Hey all: I'm working on a 'pure' python port of some existing software. Implementations of what I'm trying to accomplish are available (open source) in C++ and in Java. Which would be...
2
by: N J | last post by:
Hi, I can't figure out how to put this into code, any help would be great..thanks. Basically, if counter is over 100 then show msgbox then close. If txt.Counter > 100 Then msgbox "my message"...
458
by: wellstone9912 | last post by:
Java programmers seem to always be whining about how confusing and overly complex C++ appears to them. I would like to introduce an explanation for this. Is it possible that Java programmers...
1
by: DoctorV3774 | last post by:
We developed a small test database that we are using to attempt to connect to a wsdl to consume web services. The database form contains 3 fields and a submit button on the form. At the bottom I've...
6
by: rakeshvthu | last post by:
hi all, i am using struts for my presentation and i use html:text for my text boxes the issue is i want to disable autocomplete option for that i tried in so many different ways but it has not...
5
by: r035198x | last post by:
Setting up. Getting started To get started with java, one must download and install a version of Sun's JDK (Java Development Kit). The newest release at the time of writting this article is...
4
by: JNeko | last post by:
hello all, I have tried my hand at this for a couple hours but no luck. I am using JCreator. I used these two links for reference: http://www.tech-recipes.com/java_programming_tips1265.html...
2
by: xeltfrjw | last post by:
i have this exercise for class where im supposed to move 5 times. this is what i have so far but im told my another classmate im supposed to write it in code i dono what he means. anyone help? ...
20
by: cowboyrocks2009 | last post by:
Hi, I need help to automate my code to take data from input file. Also I need to create it as a function so that I can pass it to some other program. I am new to Java so having a bit limitation to...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.