Java grief

Status
Not open for further replies.

Space Goat

Ars Scholae Palatinae
1,039
I'm fairly new to Java. I do have a background in C++ but I seem to be having some trouble with Java. My biggest problem is that I seem to be mixing up Java and C++ and things are getting a little confusing.

I have one of my trusty C++ books with me along with some fangled Java book. I need to catch up with Java or else I will end up stalling our little team project.

I found a simple example in the C++ book and thought it would be a good idea to translate it to Java. This should have been simple,but I'm getting tons of errors and problems. Any help would be grealy appreciated.

My problem:


Create a class Vehicle which has 2 data max_speed and max_Weight. Include a method display() which does nothing.

Create a class Car that extends Vehicle . It has another data field Model and has a contructor Car() that initialises all the three fields to typical values. Override the display() method to print to the console as follows:

MODEL : XXXXX RATIO : max_weight/max_speed(to be computed)

Finally,create an independent class Car_Info containing the main(String []) function which instantiates the Car class and calls its display() method.

Do I need to put the class files in two seperate files even though one is inherited?

My initial ongoing weak attempt. (for your humor if nothing else)

code:<HR>
Code:
class Vehicle
{
    int max_speed;
    int max_weight;
    
    
    public Vehicle(int x,int y)
    {
        max_speed = x;
        max_weight = y;
    }
    
    public void display()
    {
    }
    
}<P>
class Car extends Vehicle
{
    public Car(int z)
    {
        model = z;
        
          
        model = 490;
        max_speed = 55;
        max_weight = 2000;
        ratio = max_weight/max_speed;
            
    }
    
    public void display()
    {
        System.out.println("MODEL" + model + "RATIO" + ratio);
    }
}
 

wb

Ars Legatus Legionis
25,208
Subscriptor
IIRC, "public" classes need to be in separate .java files, so assuming classes are public you should have 3 source code files when you are done.

Here's a quick implementation.


code:<HR>
Code:
(first file "Vehicle.java")
public class Vehicle
{
   protected int max_speed = 0;
   protected int max_weight = 0;<P>   public Vehicle( int maxSpeed, int maxWeight )
   {
      max_speed = maxSpeed;
      maxWeight = maxWeight;
   }<P>   public void display()
   {
   }
}<P>(second file "Car.java")
public class Car extends Vehicle
{
   String model = "";<P>   public Car()
   {
      this( "TestModel" );
   }<P>   public Car( String mdl )
   {
      this( mdl, 50, 1500 );
   }<P>   public Car( String mdl, int maxSpeed, int maxWeight );
   {
      super( maxSpeed, maxWeight );
      model = mdl;
   }<P>   public void display()
   {
      System.out.println( "MODEL: " + model +
                          "  RATIO: " +
                          (maxWeight/maxSpeed) );
   }
}<P>      
(third file "Car_Info.java")<P>public class Car_Info
{
   public static void main( String args[] )
   {
      Vehicle v = new Car( "Vogon", 35, 1200 );
      v.display();
   }
}
 

qball

Ars Tribunus Militum
2,238
They should all go in seperate files. And the OOP-nazi in me thinks that you should declair the Vehicle class to be abstract, as well as the unimplemented method. Like this:

code:<HR>
Code:
public abstract class Vehicle {
    int max_speed;
    int max_weight;<P>    public Vehicle(int x,int y) {
        max_speed = x;
        max_weight = y;
    }<P>    public abstract void display();
}

With the other classes as specified by wb. Note that the name of the file must be exactly the same as the name of it's class (case sensative).

-Q
 
Status
Not open for further replies.