|
![]() |
Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming > Application Programming | |
Simple (i think, i suck at java) code help
|
||
![]() |
|
|
LinkBack | Thread Tools |
|
|
#1 (permalink) | |||||||||||
|
Overclocker in Training
|
hey guys, i just started out in java and I'm just writing a simple program right now...maybe someone can help.
__________________if I'm trying to make a new variable called "myCar" for the class called "Car", i type Car myCar = new Car();, I know that, but I need to put the miles per gallon in at the same time, so if the car got 50 miles per gallon, i'd write "Car myCar = new Car(50);", but i dont know how to add a parameter to a class, so I get an error when I type in the 50...heres my code so far: Code:
public class Car
{
public int myGas; //amount of gas (gallons)
public int drive; //number of miles to drive
public String myCar;
public String getCar(String myCar)
{
return myCar;
}
public int addGas(int myGas)
{
return myGas;
}
public int drive(int drive)
{
return drive;
}
public static void main(String [] args)
{
Car myCar = new Car(); //# = miles per gallon
myCar.addGas(100);
myCar.drive(10);
}
}
Last edited by DanC : 10-04-07 at 07:39 PM. |
|||||||||||
|
|
|
|
|
#2 (permalink) | ||||||||||||||
|
Photography nut
![]() |
Inside the Car class you need a constructor that takes in an integer as a parameter
so something like Code:
public class Car
{
public int myGas; //amount of gas (gallons)
public int drive; //number of miles to drive
public String myCar;
// Constructor
public Car(int gas) {
this.myGas = gas;
}
public String getCar(String myCar)
{
return myCar;
}
public int addGas(int myGas)
{
return myGas;
}
public int drive(int drive)
{
return drive;
}
public static void main(String [] args)
{
Car myCar = new Car(); //# = miles per gallon
myCar.addGas(100);
myCar.drive(10);
}
}
__________________
"UNIX was never designed to keep people from doing stupid things, because that policy would also keep them from doing clever things." - Doug Gwyn Try out the latest Programming Challenge Quote:
CPU-Z Validation @ 2.97-prime95 stable 16 hours @ 1.48v Proof | CPU-Z Validation @ 3.15 Getting Mouse Side Buttons to work in Linux, Compile a custom Kernel, More
|
||||||||||||||
|
|
|
|
#3 (permalink) | ||||||||||||
|
IDDQD
|
Your methods weren't actually doing anything, so this is what I came up with.
Code:
public class Car
{
public int myGas; //amount of gas (gallons)
public int drive; //number of miles to drive
public String myCar;
private int mpg;
public Car(int gasAmt, int milesPerGallon, String name){
myGas = gasAmt;
mpg = milesPerGallon;
myCar = name;
// initializing a new car, you specify the amount of gas, the name, and the mileage.
}
public String getCar()
{
return myCar;
}
public int addGas(int moreGas)
{
myGas += moreGas; //add gas to the current amount
return myGas; // returns your new amount of gas
}
public int drive(int dist)
{
myGas - (dist*mpg); // subtract the amount of gas to drive
return myGas; // returns the amount of gas you have left
}
public static void main(String [] args)
{
Car myCar = new Car(50,25,"Viper"); //# = miles per gallon
myCar.addGas(100);
myCar.drive(10);
}
}
__________________
|
||||||||||||
|
|
|
|
#4 (permalink) | ||||||||||||||
|
Intel Overclocker
|
Quote:
__________________
A must read for understanding ram timings
|
||||||||||||||
|
|
|
|
|
#5 (permalink) | |||||||||||
|
Overclocker in Training
|
wow, thanks to everybody! i really appreciate it...now my program works, woo!
__________________
|
|||||||||||
|
|
|
|
|
#6 (permalink) | |||||||||||||
|
Programmer
|
man... I always get here too late...
![]() nice work guys! *discretely points hobo towards my linux post....*
__________________
Whats this folding I've been hearing about? Crucial Ballistix Club ![]() Member of the OCN Diablo III Club ~M Hail to the Victors M~
|
|||||||||||||
|
|
|
|
#7 (permalink) | |||||||||||||||
|
Photography nut
![]() |
Quote:
![]()
__________________
"UNIX was never designed to keep people from doing stupid things, because that policy would also keep them from doing clever things." - Doug Gwyn Try out the latest Programming Challenge Quote:
CPU-Z Validation @ 2.97-prime95 stable 16 hours @ 1.48v Proof | CPU-Z Validation @ 3.15 Getting Mouse Side Buttons to work in Linux, Compile a custom Kernel, More
|
|||||||||||||||
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
|
|