473,799 Members | 3,229 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Cars program

9 New Member
[b]I need to finish this assignment can someone help me plz.

The Car Class


The purpose of this assignment is to learn how to write your own class that serves as a template for creating objects. The Car class represents an automobile. Each car has a color, miles driven, and the amount of gasoline in the tank (gallons).

The Car class should have both a default and parameterized constructor. The default constructor should initialize all of the instance variables. (The default miles driven and gallons should be zero.) The parameterized constructor will accept a color parameter. Each constructor should call the private method drawShape() that creates the car. (The car's shape does not need to be elaborate.)

Write the methods described here:

drawShape is a private method that creates the GCompound representing the car

setColor accepts a color parameter and sets the color of the car

drive accepts miles and speed as parameters. Miles must be greater than zero. Speed must be between 0 and 100. As long as the car has gas in the tank, add the miles to the car's total miles, and reduce the amount of gasoline based on the miles per gallon, which can be estimated using the following formula:
miles per gallon = 55 - speed/2

getMilesDriven returns the number of miles the car has driven

fill accepts an amount of gas as a parameter and adds it to the car's gallons; the amount must be greater than zero

isEmpty returns true if the car's tank is empty, and false if it is not

toString returns a String containing the car's miles driven and gallons in the tank




The UML class diagram for the Car class is displayed on the right. Do not create any instance variables or methods other than those shown.

To test your Car class, create an Applet called TestDrive that extends GraphicsProgram . Create and display three cars. Use each of your constructors at least once. Fill each tank and drive each car. Then, below each car, print its information using the toString() method. (Your cars do not need to move in this assignment.)

Sample output is shown here:



Have fun!


Car
_______________ _________
- color
- miles driven
- gallons
_______________ _________

+ Car()
+ Car(color)
- drawShape()
+ setColor(color)
+ drive(miles, speed)
+ getMilesDriven( )
+ fill(amount)
+ isEmpty()
+ toString()
Feb 22 '07 #1
21 7671
abctech
157 New Member
[b]I need to finish this assignment can someone help me plz.

The Car Class


The purpose of this assignment is to learn how to write your own class that serves as a template for creating objects. The Car class represents an automobile. Each car has a color, miles driven, and the amount of gasoline in the tank (gallons).

The Car class should have both a default and parameterized constructor. The default constructor should initialize all of the instance variables. (The default miles driven and gallons should be zero.) The parameterized constructor will accept a color parameter. Each constructor should call the private method drawShape() that creates the car. (The car's shape does not need to be elaborate.)

Write the methods described here:

drawShape is a private method that creates the GCompound representing the car

setColor accepts a color parameter and sets the color of the car

drive accepts miles and speed as parameters. Miles must be greater than zero. Speed must be between 0 and 100. As long as the car has gas in the tank, add the miles to the car's total miles, and reduce the amount of gasoline based on the miles per gallon, which can be estimated using the following formula:
miles per gallon = 55 - speed/2

getMilesDriven returns the number of miles the car has driven

fill accepts an amount of gas as a parameter and adds it to the car's gallons; the amount must be greater than zero

isEmpty returns true if the car's tank is empty, and false if it is not

toString returns a String containing the car's miles driven and gallons in the tank




The UML class diagram for the Car class is displayed on the right. Do not create any instance variables or methods other than those shown.

To test your Car class, create an Applet called TestDrive that extends GraphicsProgram . Create and display three cars. Use each of your constructors at least once. Fill each tank and drive each car. Then, below each car, print its information using the toString() method. (Your cars do not need to move in this assignment.)

Sample output is shown here:



Have fun!


Car
_______________ _________
- color
- miles driven
- gallons
_______________ _________

+ Car()
+ Car(color)
- drawShape()
+ setColor(color)
+ drive(miles, speed)
+ getMilesDriven( )
+ fill(amount)
+ isEmpty()
+ toString()
Hey buddy,
This seems like your first post, welcome to TSDN and the Java forum. Before posting do take a look at the posting guidelines here
Feb 22 '07 #2
r035198x
13,262 MVP
[b]I need to finish this assignment can someone help me plz.

The Car Class


The purpose of this assignment is to learn how to write your own class that serves as a template for creating objects. The Car class represents an automobile. Each car has a color, miles driven, and the amount of gasoline in the tank (gallons).

The Car class should have both a default and parameterized constructor. The default constructor should initialize all of the instance variables. (The default miles driven and gallons should be zero.) The parameterized constructor will accept a color parameter. Each constructor should call the private method drawShape() that creates the car. (The car's shape does not need to be elaborate.)

Write the methods described here:

drawShape is a private method that creates the GCompound representing the car

setColor accepts a color parameter and sets the color of the car

drive accepts miles and speed as parameters. Miles must be greater than zero. Speed must be between 0 and 100. As long as the car has gas in the tank, add the miles to the car's total miles, and reduce the amount of gasoline based on the miles per gallon, which can be estimated using the following formula:
miles per gallon = 55 - speed/2

getMilesDriven returns the number of miles the car has driven

fill accepts an amount of gas as a parameter and adds it to the car's gallons; the amount must be greater than zero

isEmpty returns true if the car's tank is empty, and false if it is not

toString returns a String containing the car's miles driven and gallons in the tank




The UML class diagram for the Car class is displayed on the right. Do not create any instance variables or methods other than those shown.

To test your Car class, create an Applet called TestDrive that extends GraphicsProgram . Create and display three cars. Use each of your constructors at least once. Fill each tank and drive each car. Then, below each car, print its information using the toString() method. (Your cars do not need to move in this assignment.)

Sample output is shown here:



Have fun!


Car
_______________ _________
- color
- miles driven
- gallons
_______________ _________

+ Car()
+ Car(color)
- drawShape()
+ setColor(color)
+ drive(miles, speed)
+ getMilesDriven( )
+ fill(amount)
+ isEmpty()
+ toString()
What part do you need help with?
Feb 23 '07 #3
javasomething
9 New Member
I need to create my own class then transport that into a graphics program applet. I dont know how to start it.
I dont have enought time to finish it so can someone plz help me fast.
thank you very much
Feb 25 '07 #4
abctech
157 New Member
I need to create my own class then transport that into a graphics program applet. I dont know how to start it.
I dont have enought time to finish it so can someone plz help me fast.
thank you very much
Hi,
Have you started with your task?

Your class should be something like this:
class Car{

//make the variable declarations
//declare and define the specified constructors
//declare and define the specified methods

};

After having created this class you will be testing it for which you will create an Applet, but thats the second part.

Now you have all the needed details in the specification of your assignment. Make an attempt based on whatever you have understood so far and post it.
Feb 25 '07 #5
r035198x
13,262 MVP
I need to create my own class then transport that into a graphics program applet. I dont know how to start it.
I dont have enought time to finish it so can someone plz help me fast.
thank you very much
You should have started earlier to give yourself enough time to finish this. It doesn't need more than a day anyway if you paid attention in your classes.

Now no-one is going to write it for you here so you better start on it and post whenever you get a specific problem.
Feb 26 '07 #6
javasomething
9 New Member
i really need to finish this buy today plz
Feb 26 '07 #7
r035198x
13,262 MVP
i really need to finish this buy today plz
You really need to show some effort. Have you written anything at all?
Feb 26 '07 #8
javasomething
9 New Member
yes i have done this:

import java.util.*;
import java.text.*;
import java.awt.*;
import acm.graphics.*;

/*represents a car class*/
public class CAR extends GCompound
{
//declare instance variables//
private int gallons;
private boolean isEmpty;
private String toString;
private int fill;
private int drive;
private int MilesDriven;
private Color color;

/**Creates the defalut constructor*/
public CAR()
{
//Initialize instance variables
MilesDriven=0;
gallons=0;
color=Color.RED ;
toString=" ";
fill=0;
drive=0;

drawShape();
}

/**Creates the parameterized constructor*/
public CAR(int MilesDriven, int gallons, Color color)
{
//Initialize instance variables
this.MilesDrive n=MilesDriven;
this.gallons=ga llons;
this.color=colo r;

drawShape();
}

/**Sets how many gallons are needed*/
public void setGallons(int g)
{ gallons=g; }

/**Gets the gallons that we tell it to get.*/
public int getGallons()
{ return gallons; }

/**This method will return a boolean if the gas is empty.*/
public boolean isEmpty()
{
if (gallons <= 0)
{
return true;
}
else
{
return false;
}
}

/**This method will set how many miles and gallons are left.*/
public void setToString(Str ing string)
{
this.toString = string+MilesDri ven+gallons;
}

/**This method will tell us how many miles and gallons are left.*/
public String getToString()
{
return toString;
}

/**This method say to add the gallons to the car.*/
public void setFill(int amount)
{
if(amount>0)
{
amount += gallons;
}
}

/**This method will add the gallons to the car.*/
public int getFill()
{
return fill;
}

/**This method sets to how many miles the car has driven.*/
public void setMilesDriven( )
{
this.MilesDrive n=MilesDriven;
}

/**This method returns the number of miles the car has driven.*/
public int getMilesDriven( )
{
return MilesDriven;
}

/**This method will tell us the speed and miles the car has driven.*/
public void drive(double miles, int speed)
{
if(miles > 0)
{
miles += MilesDriven;

if(speed < 100)
{
MilesDriven=55-speed/2;
}
if(speed > 0)
{
//returns nothing//
}
}
}

/**This method will create the actual car and all of its parts*/
private void drawShape()
{
//Creates the top half of the car
GRect carsHead = new GRect(100, 100, 40, 40);
carsHead.setFil led(true);
carsHead.setFil lColor(color);
add(carsHead);

//Creates the bottom half of the car
GRect carsBottom = new GRect(100, 120, 60, 20);
carsBottom.setF illed(true);
carsBottom.setF illColor(Color. BLUE);
add(carsBottom) ;

//Creates the first window of the car
GRect firstWindow = new GRect(120, 105, 11, 11);
firstWindow.set Filled(true);
firstWindow.set FillColor(Color .WHITE);
add(firstWindow );

//Creates the second window of the car
GRect secondWindow = new GRect(102, 105, 11, 11);
secondWindow.se tFilled(true);
secondWindow.se tFillColor(Colo r.WHITE);
add(secondWindo w);

//Creates the first wheel of the car
GOval firstWheel = new GOval(105, 130, 20, 20);
firstWheel.setF illed(true);
firstWheel.setF illColor(Color. BLACK);
add(firstWheel) ;

//Creates the second window of the car
GOval secondWheel = new GOval(135, 130, 20, 20);
secondWheel.set Filled(true);
secondWheel.set FillColor(Color .BLACK);
add(secondWheel );
}
}


does this look correct??
Feb 28 '07 #9
RedSon
5,000 Recognized Expert Expert
This is very difficult to read with out code tags, and proper indentation. Perhaps you should try fixing that first and then more people will be able to help you.
Feb 28 '07 #10

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

Similar topics

22
3612
by: edgrsprj | last post by:
PROPOSED EARTHQUAKE FORECASTING COMPUTER PROGRAM DEVELOPMENT EFFORT Posted July 11, 2005 My main earthquake forecasting Web page is: http://www.freewebz.com/eq-forecasting/Data.html Newsgroup Readers: If you circulate copies of this report to groups of computer programmers at different universities etc. around the world then they might find the subject matter to be interesting.
0
6144
by: Tom Lee | last post by:
Hi, I'm new to .NET 2003 compiler. When I tried to compile my program using DEBUG mode, I got the following errors in the C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7 \include\xdebug file as folows. I need help to resolve them ASAP: cl /c /nologo /MDd /W3 /Od /GR /GM /Zi /GX /D "_DEBUG" /D " WIN32" /D "_W INDOWS" /D "_WINDLL" /D "_AFXDLL" /D "_MBCS" /D "_USRDLL" /
11
2615
by: christopher diggins | last post by:
I am wondering if any can point me to any open-source library with program objects for C++ like there is in Java? I would like to be able to write things like MyProgram1 >> MyProgram2 >> Fork(MyProgram3, SomeFile); If not would this be something of interest to others? Thanks in advance,
1
3276
by: Eric Whittaker | last post by:
hi all, im trying to write my first c++ program. a success, but i can't get the window to stay open after user enters input. it just automatically closes. right now the end of my program looks like this: return 0; }
9
4540
by: Hemal | last post by:
Hi All, I need to know the memory required by a c program. Is there any tool/utility which can give me the memory usage in terms of DATA segment, TEXT segment, BSS segment etc. I am working on linux platform and my target is ARM processor. But i guess it should not matter. Actually i need to know both RAM & ROM usage.
0
1389
by: mambonumba1 | last post by:
There have been many efforts to innovate automobile design funded by the NHTSA, including the work of the NavLab group at Carnegie Mellon University. Recent efforts include the highly publicized DARPA Grand Challenge race. Relatively high transportation fuel prices do not significantly reduce car usage but do make it more expensive. One environmental benefit of high fuel prices is that it is an incentive for the production of more...
7
13270
by: ibtc209 | last post by:
I just started programming in C, and I need some help with this problem. Your program will read the information about one MiniPoker hand, namely the rank and suit of the hand’s first card, and the rank and suit of its second card. Note that the two cards in a hand may be entered in any order; it’s not necessarily the case that the highest card will appear first, for example. Your program will then determine whether the hand is valid, and...
2
19354
Banfa
by: Banfa | last post by:
Posted by Banfa The previous tutorial discussed what programming is, what we are trying to achieve, the answer being a list of instructions constituting a valid program. Now we will discuss how we set about doing that. Every program starts with a specification, this may be a several hundred page document from your latest client or one small paragraph from your professor and pretty much anything in-between. The specification is very...
0
10482
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10251
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10027
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7564
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6805
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5463
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5585
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4139
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3759
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.