polymorphism, inheritance: get method is not getting anything

  • (2 Pages)
  • +
  • 1
  • 2

28 Replies - 2382 Views - Last Post: 10 December 2012 - 07:33 AM Rate Topic: ***-- 2 Votes

#1 pacololo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 25-September 11

polymorphism, inheritance: get method is not getting anything

Posted 09 December 2012 - 07:31 PM

i have traced the problem to my vehicle class, me set method is setting as it should, but my get method is not getting anything. not sure where i have gone wrong. thanks for any help or hints.

/**
 * Abstract class Vehicle - write a description of the class here
 * 
 * @author Tara Sturgis
 * @version 12/8/2012
 */
public abstract class Vehicle
{
    // instance variables - replace the example below with your own
    protected double value;
    protected String maker;
    protected String owner;
    protected String color;

    public Vehicle ()
    {
        value = 0;
        maker = "Unknown maker";
        owner = "Unknown owner";
        color = "Unknown color";
    }
    public Vehicle(String m, double v, String c, String o)
    {
      // sets values for: maker, value, color, owner
    setMaker(m); 
    setValue(v);
    setColor(c);
    setOwner(o);  
    //System.out.println(" vehicle v " + v); correct
    }
    
    // sets instance maker to param maker  
    public void setMaker(String m)  
    {  
      maker = m;  
    }  
    
     // sets instance value to param value  
    public void setValue(double v)  
    {  
      value = v;  
      System.out.println(" set v " + value);  //correct
    }  
    
    // sets instance color to param color  
    public void setColor(String c)  
    {  
      color = c;  
    }  
    
    // sets instance owner to param owner  
    public void setOwner(String o)  
    {  
      owner = o;  
    }  
     public String getMaker()   
    {  
      return maker;  
    }  
    
    // returns value of vehicle  
    public double getValue()    
    { 
      System.out.println("get" + value);  //not getting anything
      return value;  
    } 
    
    // returns color of vehicle  
    public String getColor()  
    {  
      return color;  
    }  
    
    // returns owner of vehicle  
    public String getOwner()  
    {  
      return owner;  
    }  
    

  // vehicle tostring to be overwritten  
    public abstract String toString();
}





Is This A Good Question/Topic? 0
  • +

Replies To: polymorphism, inheritance: get method is not getting anything

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: polymorphism, inheritance: get method is not getting anything

Posted 09 December 2012 - 08:03 PM

What is your problem ?
Nothing wrong with your get() methods
Was This Post Helpful? 0
  • +
  • -

#3 pacololo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 25-September 11

Re: polymorphism, inheritance: get method is not getting anything

Posted 09 December 2012 - 08:07 PM

here are the other classes, just in case. garage has a getvalue method, and homeowner has a showValue method. perhaps there is a problem in one of those that i cant find.

i need
/**
 * Main class: Driver
 * // so far prints last item on list
 * @author Tara Sturgis 
 * @version 12/8/2012
 */
public class Driver
{
  private static final String name = "Tara Sturgis";  
  private static final String title = "CMPSC 22 Final 2012";  

  public static void main(String[] args)
  {
    // prints out name and title of program 
    System.out.println(name + "\n" + title); 
    
    // instantiates a garage  
    Garage g1 = new Garage();  
    
    // instantiates a homeowner and passes it name and the garage  
    HomeOwner h1 = new HomeOwner(name, g1);  
    
    System.out.println(name + " is a homeowner with " + /*count +*/ " items in the garage:");
    // prints out list of items in garage  
    System.out.println(g1);  
    // prints out total value of items in garage  
    
   //System.out.println(h1.showValue());  // incorrect value
    
  }
}



/**
 * Write a description of class Garage here.
 * 
 * @author Tara Sturgis 
 * @version 12/8/2012
 */
public class Garage
{
    protected int count;
    protected double val;
    private Vehicle[] vehicleList;
    String x = "";
    int i = 0;

    // default constructor
    public Garage()
    {
        count = 0;   
        vehicleList = new Vehicle[6];
    }
    //adds vehicles to the list of vehicles
    public void addVehicle(Vehicle addVehicle)
    { 
      i = i;
      count ++;
      vehicleList[i] = addVehicle;  
      i++;
    }
    int j = 0;
    public double getValue()
    {
      j = j;
        double curValue = 0;
        curValue = curValue + vehicleList[j].getValue();
        //System.out.println(vehicleList[j].getValue());  //nothing
        val = val + curValue; 
        j++;
      //}  
      return val;  
    }
    public String toString()
    {
      for(int i = 0; i < vehicleList.length; i++)  
      {  
        x = x + vehicleList[i].toString();  
      }
      return x;
    }
}


/**
 * Write a description of class HomeOwner here.
 * 
 * @author Tara Sturgis
 * @version 12/8/2012
 */

import java.util.*;   // For Scanner
import java.io.*;
public class HomeOwner extends Person
{
  // Instance variables  
  // instantiates a garage 
  private Garage garage;  
  public String owner = "Tara"; 
  String [] itemList = new String [10];
  
    // Default constructor
    public HomeOwner()
    {
      super();  
      garage = null;  
      
    }
    //Constructor
    public HomeOwner(String ownerName, Garage garage)
    {
      super(ownerName); //calls superClass
      this.garage = garage;
      
      try 
      {  
        readItems();  
      } 
      catch (FileNotFoundException e) 
      {  
         e.printStackTrace();  
      }
    }

    // method to read file and creates vehicles 
    private void readItems()  throws FileNotFoundException
    {
      File inFile = new File("items.txt");
      Scanner in = new Scanner(inFile);
      
      int i = 0;
      while (in.hasNextLine())
      {
        itemList [i] = in.nextLine();
        Scanner line = new Scanner(itemList[i]); 
        line.useDelimiter(",");

        if (itemList[i].contains("Car"))
        {
          Car newCar;
          String type = line.next();
          String maker = line.next();
          double value = line.nextDouble();
          String color = line.next();
          int hP = line.nextInt();
          int doors = line.nextInt();
          newCar = new Car(maker, value, color, hP, doors, owner); 
           //System.out.println(newCar);  prints expected
          garage.addVehicle(newCar);
        }
        else if (itemList[i].contains("Bike"))
        {
          Bike newBike;
          String type = line.next();
          String maker = line.next();
          double value = line.nextDouble();
          String color = line.next();
          newBike = new Bike(maker, value, color, owner);
          //System.out.println(newBike);  prints expected
          garage.addVehicle(newBike);
        }
        else if (itemList[i].contains("MotorCycle"))
        {
          Motorcycle newMotorcycle;
          String type = line.next();
          String maker = line.next();
          double value = line.nextDouble();
          String color = line.next();
          int hP = line.nextInt();
          newMotorcycle = new Motorcycle(maker, value, color, hP, owner);
          //System.out.println(newMotorcycle);  prints expected
          garage.addVehicle(newMotorcycle);
        }
        i++;
      }
    }
    public double showValue()
    {
      double value = garage.getValue();  
      //System.out.println(value);  incorrect
      return value; 
    }
}


/**
 * Write a description of class Bike here.
 * 
 * @author Tara Sturigs 
 * @version 12/8/2012
 */
public class Bike extends Vehicle
{
    // instance variables - replace the example below with your own
    protected int wheels;

    // default constructor
    public Bike()
    {
      super();
      wheels = 2;
      color = "Red";
    }
    //constructor
    public Bike(String m, double v, String c, String o)
    {
        super(m, v, c, o);
        wheels = 2;
        //System.out.println("bike " + v);  // correct has extra
    }

    public int getWheels()  
    {  
      return wheels;  
    }  

    public String toString()
    {
        String bike = "A " + wheels + " Wheeled, " + super.getColor() + " " 
          + super.getMaker() + " bicycle.\n"; 
        return bike;
    }
}


/**
 * Write a description of class Car here.
 * 
 * @author Tara Sturgis 
 * @version 12/8/2012
 */
public class Car extends Vehicle  //subclass of the vehicle class
{
    protected int doors;
    protected int wheels;
    protected int horsepower;
    
    //default constructor
    public Car()
    {
       super();
       doors = 2;
       wheels = 4;
       setColor("Silver");
    }
    //constructor with params, sends to vehicle
    public Car(String m, double v, String c, int hP, int d, String o)
    {
      super(m, v, c, o);
      wheels = 4;
      horsepower = hP;
      doors = d;
      //System.out.println("car " + v);  // correct
    }
   // method where a string is formated and returned
    public String toString()
    {
      String car = "A " + super.getColor() + " " + super.getMaker() + " with " + doors + 
        " doors and " + horsepower + " horsepower.\n";  
      return car;   
    }
}


/**
 * Write a description of class Motorcycle here.
 * 
 * @author Tara Sturgis 
 * @version 12/8/2012
 */
public class Motorcycle extends Bike
{
    // instance variables - replace the example below with your own
    private int horsepower;

    public Motorcycle()
    {
        super();
        horsepower = 50;
        setColor("Black");
    }
    public Motorcycle(String m, double v, String c, int hP, String o)
    {
      super(m, v, c, o);
        horsepower = hP;
        //System.out.println("moto " + v);  //  correct
    }
    public String toString()
    {
      String motorcycle = "A " + super.getWheels() + " wheeled, " + super.getColor() 
        + " " + super.getMaker() + " Motor Cycle with " + horsepower + " horsepower.\n";
      return motorcycle;
    }
}




my driver is not printing value (i have it commented out in the above code): System.out.println(h1.showValue());
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: polymorphism, inheritance: get method is not getting anything

Posted 09 December 2012 - 08:11 PM

does not tell you where your probelem is

OK, your Garage has an array of Vehicle
so whith which element of this array do you have a problem ? Where is is cretaed ? It is an instance of what ? Where does it not work ?
Was This Post Helpful? 0
  • +
  • -

#5 pacololo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 25-September 11

Re: polymorphism, inheritance: get method is not getting anything

Posted 09 December 2012 - 08:20 PM

My problem is that my System.out.println(h1.showValue) in my driver is not printing anything.
I guess my problem then would be in my homeowner class showValue method maybe?
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: polymorphism, inheritance: get method is not getting anything

Posted 09 December 2012 - 08:25 PM

// System.out.println(h1.showValue());  // incorrect value 


How can it printed out an incoreect value it is commented out ???
If it wasn't commented it out what would it be showing ? Really hypotical question, and what YOU think it should show ?)
Was This Post Helpful? 0
  • +
  • -

#7 pacololo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 25-September 11

Re: polymorphism, inheritance: get method is not getting anything

Posted 09 December 2012 - 08:29 PM

its commented out cause i was trying to use println's in different places to try and isolate where the problem was. but when it is not commented out, it doesn't print anything. it should print out 27650.0
Was This Post Helpful? -1
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: polymorphism, inheritance: get method is not getting anything

Posted 09 December 2012 - 08:44 PM

This is ridiculous :)
you post here saying the good value is not printed but the place where the wrong value is printed is commented out !!! So where are we supposed to fix ?

and as 27650 is not showing is any line of the code you posted how can you expect a getter to return that value ???
Was This Post Helpful? 0
  • +
  • -

#9 pacololo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 25-September 11

Re: polymorphism, inheritance: get method is not getting anything

Posted 09 December 2012 - 08:48 PM

here it the same code without it commented out. but it does not print anything, and i cant figure out why or where in my program i went wrong. it should pring 27650.0. hope this clarifies a bit, not sure where we are misunderstanding each other.
/**
 * Main class: Driver
 * // so far prints last item on list
 * @author Tara Sturgis 
 * @version 12/8/2012
 */
public class Driver
{
  private static final String name = "Tara Sturgis";  
  private static final String title = "CMPSC 22 Final 2012";  

  public static void main(String[] args)
  {
    // prints out name and title of program 
    System.out.println(name + "\n" + title); 
    
    // instantiates a garage  
    Garage g1 = new Garage();  
    
    // instantiates a homeowner and passes it name and the garage  
    HomeOwner h1 = new HomeOwner(name, g1);  
    
    System.out.println(name + " is a homeowner with " + /*count +*/ " items in the garage:");
    // prints out list of items in garage  
    System.out.println(g1);  //works fine

    // prints out total value of items in garage  
   System.out.println(h1.showValue());  // doen't print anything
                                        //should print 27650.0
                  
  }
}


Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: polymorphism, inheritance: get method is not getting anything

Posted 09 December 2012 - 08:50 PM

and as 27650 is not showing is any line of the code you posted how can you expect a getter to return that value ??? 


Was This Post Helpful? 0
  • +
  • -

#11 pacololo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 25-September 11

Re: polymorphism, inheritance: get method is not getting anything

Posted 09 December 2012 - 08:59 PM

that value (27650.0) is the total value of the combined values for the vehicles.

the individual car values come from this text file
the 100 + 5000 + 2000 + 20000 + 500 + 50 = 27650.0
Bike,Schwinn,100,Candy Apple Red
Car,Ford,5000,Light Blue,150,4
MotorCycle,Honda,2000, Gold,75
MotorCycle,Harley,20000,Black,100
Bike,Specialized,500,White
Bike,Kiddy,50,Pink
Was This Post Helpful? 0
  • +
  • -

#12 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: polymorphism, inheritance: get method is not getting anything

Posted 09 December 2012 - 09:23 PM

First time we heard about this text file... and nothing in your posted code reads from a file so how are we supposed to guess ?
Was This Post Helpful? 0
  • +
  • -

#13 pacololo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 25-September 11

Re: polymorphism, inheritance: get method is not getting anything

Posted 09 December 2012 - 09:27 PM

oh and im am expecting the getter to return the individual values of each vehicle

sorry, im kinda all over the place, ive been working on this for days, and the file is read into an array in the homeowner class in the readItem method
Was This Post Helpful? 1
  • +
  • -

#14 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: polymorphism, inheritance: get method is not getting anything

Posted 09 December 2012 - 09:33 PM

Sorry my bad... -1 cancelled
let see your method now
Was This Post Helpful? 0
  • +
  • -

#15 pacololo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 25-September 11

Re: polymorphism, inheritance: get method is not getting anything

Posted 09 December 2012 - 09:37 PM

no worries i wasn't being clear.
/**
 * Write a description of class HomeOwner here.
 * 
 * @author Tara Sturgis
 * @version 12/8/2012
 */

import java.util.*;   // For Scanner
import java.io.*;
public class HomeOwner extends Person
{
  // Instance variables  
  // instantiates a garage 
  private Garage garage;  
  public String owner = "Tara"; 
  String [] itemList = new String [10];
  
    // Default constructor
    public HomeOwner()
    {
      super();  
      garage = null;  
      
    }
    //Constructor
    public HomeOwner(String ownerName, Garage garage)
    {
      super(ownerName); //calls superClass
      this.garage = garage;
      
      try 
      {  
        readItems();  
      } 
      catch (FileNotFoundException e) 
      {  
         e.printStackTrace();  
      }
    }

    // method to read file and creates vehicles 
    private void readItems()  throws FileNotFoundException
    {
      File inFile = new File("items.txt");
      Scanner in = new Scanner(inFile);
      
      int i = 0;
      while (in.hasNextLine())
      {
        itemList [i] = in.nextLine();
        Scanner line = new Scanner(itemList[i]); 
        line.useDelimiter(",");

        if (itemList[i].contains("Car"))
        {
          Car newCar;
          String type = line.next();
          String maker = line.next();
          double value = line.nextDouble();
          String color = line.next();
          int hP = line.nextInt();
          int doors = line.nextInt();
          newCar = new Car(maker, value, color, hP, doors, owner); 
          garage.addVehicle(newCar);
        }
        else if (itemList[i].contains("Bike"))
        {
          Bike newBike;
          String type = line.next();
          String maker = line.next();
          double value = line.nextDouble();
          String color = line.next();
          newBike = new Bike(maker, value, color, owner);
          garage.addVehicle(newBike);
        }
        else if (itemList[i].contains("MotorCycle"))
        {
          Motorcycle newMotorcycle;
          String type = line.next();
          String maker = line.next();
          double value = line.nextDouble();
          String color = line.next();
          int hP = line.nextInt();
          newMotorcycle = new Motorcycle(maker, value, color, hP, owner);
          garage.addVehicle(newMotorcycle);
        }
        i++;
      }
    }
    public double showValue()
    {
      double value = 0;
      value = value + garage.getValue();  
      //System.out.println(value);  incorrect
      return value; 
    }
}



Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2