Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Having trouble printing object information in main class

  1. #1
    Junior Member
    Join Date
    Sep 2009
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Having trouble printing object information in main class

    I am having trouble printing object information in my main class. The class vehicleInfo is my main class and I have instantiated objects from the other classes. I'm trying to print that information using the toPrint() method in the main class. The class PassengerVehicle extends the class Vehicle and the class SUV extends PassengerVehicle. If I want to print the information for, say, suv1, how do I make sure that all the information from Vehicle and PassengerVehicle get printed for suv1? How should the toPrint() method in the class SUV look like and how do I call the toPrint() method for that object in the main class vehicleInfo?

    HTML Code:
    public class vehicleInfo {
    	
    	public static void main(String[] args) {
    		
    		//instantiating the objects.
    		
    		SUV suv1 = new SUV("","silver", "Chevrolet", 5 );
    		SUV suv2 = new SUV("", "white", "Chevrolet", 5);
    		
    		Truck truck1 = new Truck("", "green", "Chevrolet", 5);
    		Truck truck2 = new Truck("", "black", "Chevrolet", 3 );
    		
    		Car car1 = new Car("", "black", "Chevrolet", 4);
    		Car car2 = new Car("", "ebony", "Chevrolet", 2);
    		
    		
    		//printing the information associated with each vehicle object.
    		suv1.toPrint();
    		System.out.println();
    		
    		suv2.toPrint();
    		System.out.println();
    		
    		truck1.toPrint();
    		System.out.println();
    		
    		truck2.toPrint();
    		System.out.println();
    		
    		car1.toPrint();
    		System.out.println();
    		
    		car2.toPrint();
    		System.out.println();
    	}
    
    
    
    
    public class Vehicle {
    	
    	//instance variables
    	String VIN;
    	String color, maker;
    	
    	
    	
    	//constructors
    	public Vehicle() {//default constructor
    		VIN = "";
    		color = "";
    		maker = "";
    	}
    	
    	public Vehicle(String inVIN, String c, String m) {//constructor with parameters VIN, color and maker.
    		VIN = inVIN;
    		color = c;
    		maker = m;
    	}
    	
    	
    	
    	//accessors
    	public String getVIN() {
    		return VIN;
    	}
    	
    	public String getColor() {
    		return color;
    	}
    	
    	public String getMaker() {
    		return maker;
    	}
    	
    	
    	
    	//mutators
    	public void setVIN(String inVIN) {
    		VIN = inVIN;
    	}
    	
    	public void setColor(String inColor) {
    		color = inColor;
    	}
    	
    	public void setMaker(String inMaker) {
    		maker = inMaker;
    	}
    	
    	
    	
    	//toString method
    	/*public String toString() {
    		String outPut = "The vehicle manufacturer is " + maker + ". It is " + color + " and has VIN number " + VIN + ".";
    		return outPut;
    	}*/
    	
    	//toPrint method.
    	public void toPrint() {
    		System.out.println("The vehicle manufacturer is " + maker + ". It is " + color + " and has VIN number " + VIN + ".");
    	}
    }
    
    public class PassengerVehicle extends Vehicle {
    
    	//instance variable
    	int numPassengers;
    	
    	//constructors
    	public PassengerVehicle() {//default constructor
    		
    		super();
    		numPassengers = 0;
    	}
    	
    	
    
    
          public PassengerVehicle(String inVIN, String c, String m, int passengersIn) {//constructor with parameters
    		
    		super(inVIN, c, m);
    		numPassengers = passengersIn;
    	}
    	
    	
    	//accessors
    	public int getNumPassengers() {
    		return numPassengers;
    	}
    	
    	//mutator
    	public void setNumPassengers(int passengersIn) {
    		numPassengers = passengersIn;
    	}
    	
    	//toPrint method
    	public void toPrint() {
    		System.out.println("This vehicle can seat " + numPassengers + "passengers.");
    	}
    }
    
    
    public class SUV extends PassengerVehicle {
    	
    	//no instance variables
    	
    	//constructors
    	public SUV() {//default constructor
    		super();
    	}
    	
    	public SUV(String inVIN, String c, String m, int passengersIn) {//constructor with parameters.
    		super(inVIN, c, m, passengersIn);
    	}
    	
    	//accessors
    	
    	//mutators
    	
    	//toPrint method.
    	public void toPrint() {
    		//PassengerVehicle.toPrint();
    	}
    }
    
    }
    Last edited by KingLane; October 11th, 2009 at 06:41 PM.


  2. #2
    Junior Member
    Join Date
    Sep 2009
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Having trouble printing object information in main class

    Okay, a friend helped me and the program is now working. I have reprinted the methods with the correct toPrint() formats below.

    HTML Code:
    public class vehicleInfo {
    	
    	public static void main(String[] args) {
    		
    		//instantiating the objects.
    		
    		SUV suv1 = new SUV("","silver", "Chevrolet", 5 );
    		SUV suv2 = new SUV("", "white", "Chevrolet", 5);
    		
    		Truck truck1 = new Truck("", "green", "Chevrolet", 5);
    		Truck truck2 = new Truck("", "black", "Chevrolet", 3 );
    		
    		Car car1 = new Car("", "black", "Chevrolet", 4);
    		Car car2 = new Car("", "ebony", "Chevrolet", 2);
    		
    		
    		//printing the information associated with each vehicle object.
    		suv1.toPrint();
    		System.out.println();
    		
    		suv2.toPrint();
    		System.out.println();
    		
    		truck1.toPrint();
    		System.out.println();
    		
    		truck2.toPrint();
    		System.out.println();
    		
    		car1.toPrint();
    		System.out.println();
    		
    		car2.toPrint();
    		System.out.println();
    	}
    }
    
    public class Vehicle {
    	
    	//instance variables
    	String VIN;
    	String color, maker;
    	
    	
    	
    	//constructors
    	public Vehicle() {//default constructor
    		VIN = "";
    		color = "";
    		maker = "";
    	}
    	
    	public Vehicle(String inVIN, String c, String m) {//constructor with parameters VIN, color and maker.
    		VIN = inVIN;
    		color = c;
    		maker = m;
    	}
    	
    	
    	
    	//accessors
    	public String getVIN() {
    		return VIN;
    	}
    	
    	public String getColor() {
    		return color;
    	}
    	
    	public String getMaker() {
    		return maker;
    	}
    	
    	
    	
    	//mutators
    	public void setVIN(String inVIN) {
    		VIN = inVIN;
    	}
    	
    	public void setColor(String inColor) {
    		color = inColor;
    	}
    	
    	public void setMaker(String inMaker) {
    		maker = inMaker;
    	}
    	
    	
    	
    	//toString method
    	/*public String toString() {
    		String outPut = "The vehicle manufacturer is " + maker + ". It is " + color + " and has VIN number " + VIN + ".";
    		return outPut;
    	}*/
    	
    	//toPrint method.
    	public void toPrint() {
    		System.out.println("The vehicle manufacturer is " + maker + ". It is " + color + " and has VIN number " + VIN + ".");
    		//System.out.println(Vehicle(inVIN, c, m));
    	}
    }
    
    public class PassengerVehicle extends Vehicle {
    
    	//instance variable
    	int numPassengers;
    	
    	//constructors
    	public PassengerVehicle() {//default constructor
    		
    		super();
    		numPassengers = 0;
    	}
    	
    	public PassengerVehicle(String inVIN, String c, String m, int passengersIn) {//constructor with parameters
    		
    		super(inVIN, c, m);
    		numPassengers = passengersIn;
    	}
    	
    	
    	//accessors
    	public int getNumPassengers() {
    		return numPassengers;
    	}
    	
    	//mutator
    	public void setNumPassengers(int passengersIn) {
    		numPassengers = passengersIn;
    	}
    	
    	//toPrint method
    	public void toPrint() {
    		super.toPrint();
    		System.out.println("This vehicle can seat " + numPassengers + " passengers.");
    	}
    }
    
    public class SUV extends PassengerVehicle {
    	
    	//no instance variables
    	
    	//constructors
    	public SUV() {//default constructor
    		super();
    	}
    	
    	public SUV(String inVIN, String c, String m, int passengersIn) {//constructor with parameters.
    		super(inVIN, c, m, passengersIn);
    	}
    	
    	//accessors
    	
    	//mutators
    	
    	//toPrint method.
    	public void toPrint() {
    		super.toPrint();
    	}
    }

Similar Threads

  1. Object o = new Object(); need help
    By zeeshanmirza in forum Object Oriented Programming
    Replies: 11
    Last Post: January 6th, 2010, 10:01 PM
  2. Main Class Not Found Problem
    By shadow in forum Java Theory & Questions
    Replies: 3
    Last Post: September 29th, 2009, 09:42 AM
  3. could not find the main class
    By Tisofa in forum Object Oriented Programming
    Replies: 1
    Last Post: September 27th, 2009, 02:58 AM
  4. [SOLVED] Trouble with draw and fillRect in pyramid logic using nested loop
    By LiquidMetal in forum Loops & Control Statements
    Replies: 4
    Last Post: April 27th, 2009, 03:25 AM
  5. Trouble in downloading java
    By captjade in forum Java Theory & Questions
    Replies: 6
    Last Post: March 3rd, 2009, 04:16 PM