java code problem


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 7 of 7

Thread: java code problem

Hybrid View

  1. #1
    Join Date
    Oct 2004
    Posts
    11

    Arrow java code problem

    Hello!
    I try to write a program car registration. I have two class:
    1-class Car
    which has (model,color,motorPower). 2-class CarTable which has an array whit 50
    place(we can put the cars and print out them)
    class Car{
    private String model;
    private String color;
    private double motorPower;
    public Car(String iModel, String iColor, double iMotorPower){
    .......
    }//methodes
    public String getModel(){
    public String getColor(){
    public double getPower(){
    * public void setColor(String ){
    * public String getCar(int n){
    ------------------------------------------------------
    class CarTable{}
    private String[]carVector;
    private int nbr;
    public CarTable (){
    carVector= new String[50];
    nbr=0;{
    *public .....addCar(“ model”,”color”,”power”){
    ---------------------------------------------------------------------------------
    The test program must be like this;
    Public static void main(......)
    Car b1 = new Car("Benz","blue",190);
    Car b2;
    CarTable myTable= new CarTable();
    *b2 = myTable.getCar(1);
    *myTable.addCar(new Car("Volvo","red",120));
    *b2.setColor("green");
    ----------------------------------------------------------
    MY PROBLEM is the sentences which have stars(*)
    How can I complate class CarTable whit addCar() methode and
    Class Car whit getCar() and setColor()
    thanks

  2. #2
    Join Date
    Oct 2004
    Location
    UK
    Posts
    12
    I don't think your design is right. Basically, your Car class encapsulates a record, and your CarTable class encapsulates a collection of those records. Therefore, I don't see why you'd have a getCar() method in the Car class, it belongs in the containing class, CarTable. Something like this:

    Code:
    import java.util.*;
    
    public class TestHarness
    {
        public static void main(String[] args)
        {
            CarTable myTable = new CarTable();
            Car b1 = new Car("Benz", "blue", 190);
            myTable.addCar(b1);
            Car b2 = new Car("Volvo", "red", 120);
            b2.setColor("green");
            myTable.addCar(b2);
            myTable.addCar("Nissan", "black", 150);
            
            /*
               See class CarTable for info on the following
            */
            
            Iterator it = myTable.getCarIterator();
            while (it.hasNext())
            {
                Car car = (Car) it.next();
                System.out.println(car.getModel() + " " + car.getColor() + " " + car.getMotorPower());
            }     
        }
    }
    
    class Car
    {
        private String model;
        private String color;
        private double motorPower;
        
        // I don't like not providing a default constructor :)
        
        private Car() {}
        
        public Car(String model, String color, double motorPower)
        {
            this.model = model;
            this.color = color;
            this.motorPower = motorPower;
        }
        
        // accessor methods
        
        public String getModel()
        {
            return model;
        }
        
        public String getColor()
        {
            return color;
        }
        
        public double getMotorPower()
        {
            return motorPower;
        }
        
        public void setModel(String model)
        {
            this.model = model;
        }
        
        public void setColor(String color)
        {
            this.color = color;
        }
        
        public void setMotorPower(double motorPower)
        {
            this.motorPower = motorPower;
        }
    }
    
    class CarTable
    {
        /* 
           a list-based collection is better than an array because
           it can grow easily. ArrayList would be the collection of
           choice, but Vector (a synchronised ArrayList) will do
           just as well
        */
        
        private Vector carVector;
    
        public CarTable()
        {
            carVector = new Vector();
        }
     
        /*
           Two implementations of addCar(). One allowing a Car object
           to be passed, the other allowing the various data members 
           of  car to be passed.
        */
        
        public void addCar(String model, String color, double motorPower)
        {
            carVector.add(new Car(model, color, motorPower));
        }
        
        public void addCar(Car car)
        {
            carVector.add(car);
        }
     
        /* 
           This method returns a Car object by an index number. This isn't
           a particularly good idea. What would be better would be if a
           collection could be obtained, or better yet, an iterator...
        */
        
        public Car getCar(int index)
        {
            return (Car) carVector.get(index);
        }
        
        /*
           ...like this.
        */
        
        public Iterator getCarIterator()
        {
            return carVector.iterator();
        }
    }

  3. #3
    Join Date
    Oct 2004
    Posts
    11
    Hi again!
    Thank you very much , you solved my problem.
    thanks
    Morgan

  4. #4
    Join Date
    Oct 2004
    Location
    UK
    Posts
    12
    but can you tell me how can I write the code if I have
    a private int[]vector=new int[100];//vector for 100 cars
    how can I write addCar() and getCar() methodes.
    The data structure you use to hold the Car objects needs to be of the Car type; you can't use an int array to store the cars, you have to use a Car array.

    Code:
    public class TestHarness
    {
        public static void main(String[] args)
        {
            CarTable myTable = new CarTable();
            Car b1 = new Car("Benz", "blue", 190);
            myTable.addCar(b1);
            Car b2 = new Car("Volvo", "red", 120);
            b2.setColor("green");
            myTable.addCar(b2);
            myTable.addCar("Nissan", "black", 150);
            
            for (int i = 0; i < myTable.getCarCount(); i++)
            {
                System.out.println(myTable.getCar(i));
            }
        }
    }
    
    class Car
    {
        private String model;
        private String color;
        private double motorPower;
        
        // I don't like not providing a default constructor :)
        
        private Car() {}
        
        public Car(String model, String color, double motorPower)
        {
            this.model = model;
            this.color = color;
            this.motorPower = motorPower;
        }
        
        // accessor methods
        
        public String getModel()
        {
            return model;
        }
        
        public String getColor()
        {
    
            return color;
        }
        
        public double getMotorPower()
        {
            return motorPower;
        }
        
        public void setModel(String model)
        {
            this.model = model;
        }
        
        public void setColor(String color)
        {
            this.color = color;
        }
        
        public void setMotorPower(double motorPower)
        {
            this.motorPower = motorPower;
        }
        
        public String toString()
        {
            return new String(model + " " + color + " " + motorPower);
        }
    }
    
    class CarTable
    { 
        private Car[] carVector;
        private int carCount = 0;
    
        public CarTable()
        {
            carVector = new Car[100];
        }
       
        public void addCar(String model, String color, double motorPower)
        {
            carVector[carCount] = new Car(model, color, motorPower);
            carCount++;
        }
        
        public void addCar(Car car)
        {
            carVector[carCount] = car;
            carCount++;
        }
        
        public Car getCar(int index)
        {
            return carVector[index];
        }
        
        public Car[] getCars()
        {
            return carVector;
        }
        
        public int getCarCount()
        {
            return carCount;
        }
    }

  5. #5
    Join Date
    Oct 2004
    Posts
    11
    // CarTest class must be like this. we should not change it.
    class CarTest

    {
    public static void main(String[] args)
    {
    System.out.println();
    Car b1 = new Car("Audi", "blue", 180);
    Car b2, b3;
    System.out.println(b1);
    System.out.println("--------------------------------------");
    System.out.println("List of cars: ");
    System.out.println("--------------------------------------");
    System.out.println("model"+"\t"+"\t"+"color"+"\t"+"\t"+"power");
    System.out.println("------"+" \t "+"-----"+" \t "+"----");
    CarTable myCar= new CarTable();

    myCar.addCar(b1);

    myCar.addCar(new Car("Volvo","red",120));

    myCar.addCar(new Car("Ford","black",137));
    myCar.addCar(new Car("Saab","white",125));

    System.out.println(myCar);

    b2 = myCar.getCar(1);

    System.out.println("change color " + b2.getColor());
    System.out.println( "and vi change it to green:\n");

    b2.setColor("green");

    System.out.println(myCar.getCar(1));

    b3 =myCar.getCar(2);

    System.out.println( "\n third cars power is: "+b3.getPower());



    System.out.println( "and we change it to 170:\n");



    myCar.getCar(2).setPower(170);

    System.out.println(myCar.getCar(2));
    System.out.println();}}

  6. #6
    Join Date
    Oct 2004
    Location
    UK
    Posts
    12
    System.out.println(myCar);

    What do you want that line to do? Its trying to print an instance of CarTable. Unless you define a toString() method for CarTable, it'll just print the internal object name, e.g. CarTable@18a992f.

    The rest of your code works with the code I supplied apart from the getMotorPower and setMotorPower methods must be renamed to getPower and setPower. Also, doing getCar(1) actually gives the second item in the car array, because array indexes start at 0.

  7. #7
    Join Date
    Oct 2004
    Posts
    11
    Hi!
    // my test class
    class CarTest

    {
    public static void main(String[] args)
    {
    System.out.println();
    Car b1 = new Car("Audi", "blue", 180);
    Car b2, b3;
    System.out.println(b1);

    System.out.println("model"+"\t"+"\t"+"color"+"\t"+"\t"+"power");

    CarTable myTable= new CarTable();

    myTable.addCar(b1);

    myTable.addCar(new Car("Volvo","red",120));

    myTable l.addCar(new Car("Ford","black",137));
    myTable.addCar(new Car("Saab","white",125));

    System.out.println(myTable );//?????????????

    b2 = myTable .getCar(1);

    System.out.println("chage color " + b2.getColor());
    System.out.println( "we chane it to green:\n");

    b2.setColor("green");

    System.out.println(myTable.getCar(1));

    b3 =myTable.getCar(2);

    System.out.println(b3.getPower());

    myTable.getCar(2).setPower(170);

    System.out.println(myTable.getCar(2));
    System.out.println();


    }

    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center