Overclock.net - Overclocking.net
     
 
Home Gallery Reviews Blogs Register Today's Posts Mark Forums Read Members List


Go Back   Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming > Application Programming

Reply
 
LinkBack Thread Tools
Old 10-04-07   #1 (permalink)
Overclocker in Training
 
intel nvidia

Join Date: Jul 2006
Location: Massachusetts
Posts: 200

Rep: 1 DanC Unknown
Unique Rep: 1
Trader Rating: 0
Default Simple (i think, i suck at java) code help

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);
	}
}
btw, i just started java like, 3 days ago and i know more than my teacher already, but its still not much. what i did up there was stuff i learned on ym own since i started, lol
__________________
System: Beast
CPU
Core 2 Quad Q6600
Motherboard
DFI DK P35
Memory
OCZ 4GB DDR2 800
Graphics Card
BFG Tech 9600GT 512MB
Hard Drive
320GB Seagate 7200rpm
Sound Card
SB Live! 24bit
Power Supply
OCZ SilentXStream 600w
Case
NZXT TRINITY STEEL ATX
OS
Vista x64
Monitor
Samsung 2443BWX 24"

Last edited by DanC : 10-04-07 at 07:39 PM.
DanC is offline   Reply With Quote
Old 10-04-07   #2 (permalink)
Photography nut
 
dangerousHobo's Avatar
 
amd nvidia

Join Date: Dec 2005
Location: ~/
Posts: 3,484

FAQs Submitted: 7
Folding Team Rank: 451
Trader Rating: 0
Default

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:
Originally Posted by Melcar
Only one reasonable way to solve this... a dance off.

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

System: Anomaly
CPU
Athlon 3700 SD(KACAE)0546 @3.02ghz
Motherboard
DFI UT nF4 Ultra-D
Memory
G.Skill 2x512 UTT(BH-5)
Graphics Card
evga 6800gs
Hard Drive
Maxtor 300GB + WD 250GB
Sound Card
onboard
Power Supply
Ultra 500w V-series
Case
one from Ultra
CPU cooling
Big Typhoon
GPU cooling
80mm fan mounted on
OS
Arch64 & Slackware 12.1
Monitor
Acer AL2216W 22" WS LCD
dangerousHobo is offline I fold for Overclock.net Overclocked Account dangerousHobo's Gallery   Reply With Quote
Old 10-04-07   #3 (permalink)
IDDQD
 
C-bro's Avatar
 
intel nvidia

Join Date: Jan 2006
Location: Hamilton, ON
Posts: 1,912

Rep: 296 C-bro is a proven memberC-bro is a proven memberC-bro is a proven member
Unique Rep: 226
FAQs Submitted: 6
Folding Team Rank: 289
Hardware Reviews: 9
Trader Rating: 1
Default

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

System: RAID0R
CPU
Intel E2180 3.33GHz
Motherboard
Asus P5K-E/WIFI-AP vMod
Memory
2GB Kingmax DDR2-1066
Graphics Card
EVGA 8800GT
Hard Drive
2x250GB WD+500GB 7200.11
Sound Card
SB Audigy 2
Power Supply
Corsair CMPSU-550VX
CPU cooling
Arctic Cooling Freezer 7 Pro
GPU cooling
Zalman VF900-Cu
OS
Windows Vista Business x64
Monitor
HP F2105 21" & Samsung 712N
C-bro is offline I fold for Overclock.net Overclocked Account C-bro's Gallery   Reply With Quote
Old 10-04-07   #4 (permalink)
Intel Overclocker
 
ra2shadow's Avatar
 
intel nvidia

Join Date: Sep 2006
Location: Lanark, Scotland
Posts: 618

Rep: 53 ra2shadow is acknowledged by some
Unique Rep: 47
Trader Rating: 2
Default

Quote:
Originally Posted by C-bro View Post
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);
    }
}
Im rep+ on that i was in the middle of typing that......quickly done.

System: Well its a system I suppose
CPU
Pentium C2D E6600
Motherboard
MSI-p6n sli platinum
Memory
2g Corsair CM2X PC2 6400
Graphics Card
MSI 7900GTO
Hard Drive
TB of space spread over 3 hdd.
Sound Card
on board
Power Supply
Hyper Type R 580w
Case
Apevia X-crusier silver
CPU cooling
Thermaltake Water Cooling
GPU cooling
Stock
OS
xp pro sp2
Monitor
video seven 20.1"
ra2shadow is offline   Reply With Quote
Old 10-04-07   #5 (permalink)
Overclocker in Training
 
intel nvidia

Join Date: Jul 2006
Location: Massachusetts
Posts: 200

Rep: 1 DanC Unknown
Unique Rep: 1
Trader Rating: 0
Default

wow, thanks to everybody! i really appreciate it...now my program works, woo!
__________________
System: Beast
CPU
Core 2 Quad Q6600
Motherboard
DFI DK P35
Memory
OCZ 4GB DDR2 800
Graphics Card
BFG Tech 9600GT 512MB
Hard Drive
320GB Seagate 7200rpm
Sound Card
SB Live! 24bit
Power Supply
OCZ SilentXStream 600w
Case
NZXT TRINITY STEEL ATX
OS
Vista x64
Monitor
Samsung 2443BWX 24"
DanC is offline   Reply With Quote
Old 10-04-07   #6 (permalink)
Programmer
 
kdbolt70's Avatar
 
intel ati

Join Date: May 2007
Location: Walled Lake, MI
Posts: 1,119

Rep: 127 kdbolt70 is acknowledged by manykdbolt70 is acknowledged by many
Unique Rep: 92
Folding Team Rank: 284
Trader Rating: 1
Default

man... I always get here too late...

nice work guys!


*discretely points hobo towards my linux post....*
__________________

~M Hail to the Victors M~

System: It's about time!
CPU
Q6600 G0 @3.3Ghz
Motherboard
Gigabyte P35-DS3L
Memory
2Gb Ballistix DDR2 800 @915Mhz
Graphics Card
Sapphire 2900Pro Flashed to XT
Hard Drive
Seagate Barracuda 320Gb
Sound Card
Onboard
Power Supply
Corsair HX 620W
Case
CM 690
CPU cooling
Tuniq Tower 120
GPU cooling
stock
OS
Vista Business and VMWare Ubuntu
Monitor
Acer AL2223W 22"
kdbolt70 is offline I fold for Overclock.net   Reply With Quote
Old 10-04-07   #7 (permalink)
Photography nut
 
dangerousHobo's Avatar
 
amd nvidia

Join Date: Dec 2005
Location: ~/
Posts: 3,484

FAQs Submitted: 7
Folding Team Rank: 451
Trader Rating: 0
Default

Quote:
Originally Posted by kdbolt70 View Post
man... I always get here too late...

nice work guys!


*discretely points hobo towards my linux post....*
I'll add you to my queue of people to help tonight.
__________________
"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:
Originally Posted by Melcar
Only one reasonable way to solve this... a dance off.

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

System: Anomaly
CPU
Athlon 3700 SD(KACAE)0546 @3.02ghz
Motherboard
DFI UT nF4 Ultra-D
Memory
G.Skill 2x512 UTT(BH-5)
Graphics Card
evga 6800gs
Hard Drive
Maxtor 300GB + WD 250GB
Sound Card
onboard
Power Supply
Ultra 500w V-series
Case
one from Ultra
CPU cooling
Big Typhoon
GPU cooling
80mm fan mounted on
OS
Arch64 & Slackware 12.1
Monitor
Acer AL2216W 22" WS LCD
dangerousHobo is offline I fold for Overclock.net Overclocked Account dangerousHobo's Gallery   Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools



All times are GMT -4. The time now is 02:36 AM.


Overclock.net is a Carbon Neutral Site Creative Commons License Internet Security By ControlScan

Terms of Service / Forum Rules | Privacy Policy | Advertising | Become an Official Vendor
Copyright © 2008 Shogun Interactive Development. Most rights reserved.
Page generated in 0.33203 seconds with 8 queries