7 Replies - 526 Views - Last Post: 30 May 2012 - 07:47 PM Rate Topic: -----

#1 rosepetalpowder  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 65
  • Joined: 28-February 12

problem - understanding main method

Posted 30 May 2012 - 02:10 AM

Write a main which will
o Read the data on each vehicle and create an instance of the vehicle
o Store the vehicle data in an array
o After all data has been read, will print the data on each vehicle

What does this mean? Do I need a scanner?
Is This A Good Question/Topic? 0
  • +

Replies To: problem - understanding main method

#2 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,056
  • Joined: 28-December 10

Re: problem - understanding main method

Posted 30 May 2012 - 02:17 AM

Yes. You need:

- input(Scanner,BufferedReader though I prefer the former)
- an array
- an object of class vehicle
- a main class to input vehicle info and print them

It's all in your specifications. Why not try coding it?
Was This Post Helpful? 0
  • +
  • -

#3 rosepetalpowder  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 65
  • Joined: 28-February 12

Re: problem - understanding main method

Posted 30 May 2012 - 03:49 AM

View PostfromTheSprawl, on 30 May 2012 - 02:17 AM, said:

Yes. You need:

- input(Scanner,BufferedReader though I prefer the former)
- an array
- an object of class vehicle
- a main class to input vehicle info and print them

It's all in your specifications. Why not try coding it?



 public static void main(String[] args)throws IOException{

        String manufacturer,colour,size,stringYear,stringCubic,stringSeats;
        Car bigCar = new Car(manufacturer,year,colour,size);






this is part of the code from main method. when I am trying to create a new car it keeps saying variables are not initialised.

what does this mean?
Was This Post Helpful? 0
  • +
  • -

#4 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2204
  • View blog
  • Posts: 5,236
  • Joined: 10-September 10

Re: problem - understanding main method

Posted 30 May 2012 - 04:18 AM

Uninitialized variable:

String colour;

Initialized variable:

String colour = "Red";

- or -

String colour;
colour = "Red";
Was This Post Helpful? 2
  • +
  • -

#5 rosepetalpowder  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 65
  • Joined: 28-February 12

Re: problem - understanding main method

Posted 30 May 2012 - 07:26 PM

So I have another question.

I have a main method with scanner to ask questions.Then in subclass I got printData method.

I need the data entered in the scanner to printed in the text terminal by using the printdata method.
How?

I tried the following.

 InputStreamReader console = new InputStreamReader (System.in);  //converter is a variable 
        BufferedReader inReader = new BufferedReader(console);         //inReader is variable
        
        System.out.println("Please select your Vehicle - car, minivan or suv");
        newVehicle = inReader.readLine();
        
        if (newVehicle.equals("car"))
        {
            System.out.println("Please enter Manufacturer:");
              manufacturer = inReader.readLine();
              
              System.out.println("Please enter year:");
              stringYear = inReader.readLine();
              year = Integer.parseInt(stringYear);
              
              System.out.println("Please enter Colour:");
              colour = inReader.readLine();
              
              System.out.println("Please enter Size:");
              size = inReader.readLine();
              
              cars.add(new Car(manufacturer,year,colour,size)); //it adds all cars from car object into an array. 
                                                                          
              newVehicle.printData();  


code from car

 public String toString()
    {
        String line1 = "Your car details are:"; // assigning value for line1 which has a string data type
        
        // we return the values using the return statement
        
        return line1 + "\n Manufacturer:" +manufacturer+  //  \n puts the string to next line 
        "\n Year:" +year+                                //and + links the string with declared constants
        "\n Colour:" +colour+
        "\n Size:" +size;
    }
    
    // This is the printData method.We print the data using the toString method.
    public void printData()
    {
        // The println statement takes the string information from the toString method 
        //and displays on the screen
        System.out.println(toString()); // invoking the toString method
    } 


Please advise.

This post has been edited by rosepetalpowder: 30 May 2012 - 07:27 PM

Was This Post Helpful? 0
  • +
  • -

#6 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,056
  • Joined: 28-December 10

Re: problem - understanding main method

Posted 30 May 2012 - 07:40 PM

Setters and getters would help.

public class Car{
private String color;

public void setColor(String color){
this.color = color;
}

public String getColor(){
return color;
}
}



Use those to add data to your class of Car and print their details.
Was This Post Helpful? 1
  • +
  • -

#7 Kakerergodt  Icon User is offline

  • D.I.C Head

Reputation: 87
  • View blog
  • Posts: 201
  • Joined: 01-May 12

Re: problem - understanding main method

Posted 30 May 2012 - 07:45 PM

First you need to decide if you want to save one new vehicle or several vehicles, no use in having, what I assume is a List(cars) for only one car then you could just save the reference to the new car in a single variable.

Right now you are trying to call the printData() method from variable "newVehicle" which is a String not a Car. What you want to do is create a loop that iterates through all of the cars in the car list.

Ofcourse now you only have one car, so you'll need to give the user the opportunity to add more. One way to do that is create a while loop and check if the user types a specified String before jumping out of the loop.

while(true) //Create cars
{
  <code for creating cars>
  <ask if user wants to add another, if not then "break".>
}

for(Car c : cars) //Print info about cars
{
   c.printData();
}

This post has been edited by Kakerergodt: 30 May 2012 - 07:46 PM

Was This Post Helpful? 1
  • +
  • -

#8 rosepetalpowder  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 65
  • Joined: 28-February 12

Re: problem - understanding main method

Posted 30 May 2012 - 07:47 PM

thnx I figured it out :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1