9 Replies - 776 Views - Last Post: 23 October 2012 - 11:32 PM Rate Topic: -----

#1 realnoob  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 14-October 12

Need help with Arrays

Posted 23 October 2012 - 10:10 PM

Hello:

This assignment is a continuation of my two previous assignments.... This is assignment is requiring me to have arrays in my code....
my previous assignments http://www.dreaminco...ds-and-classes/


Instructions

1.Modify the Fleet class by:
A)removing the instance variables primaryCar and secondaryCar and replacing it with a single array of Cars.

B)removing the corresponding accessors and mutators and implementing an add method and a remove method. Both should take a Car as an argument, return nothing (void), and add or remove the Car to/from the array instance variable.

2.Add a toString() method to the Car class. This method should return a string containing the type of car, its mileage, and its fuel tank level.

3.Modify the main method from PA5 to create a few cars and add several to your fleet, drive them around, fill them up with gas, and print them out (causing their toString methods to be invoked).

and this is my my fleet class
public class Fleet {
  private Car primaryCar;
  private Car secondaryCar;
  
  
  public Car getPrimaryCar(){
    return primaryCar  ;
  }
  
  public void setPrimaryCar ( Car newCar) {
      primaryCar = newCar;
  }
    
  public Car getSecondaryCar(){
    return secondaryCar;
  }
  
  public void setSecondaryCar(Car newCar) {
    secondaryCar = newCar;
  }
  
  public int size(){
    int size = 0;
    if (primaryCar != null )
      size++;
    if (secondaryCar != null)
      size++;
    return size;
  }
  
   
  
   public String toString(){
     String out = "this fleet has " + size() + " cars. \n";
       if (primaryCar != null ){
      out += "Primary car has " + primaryCar.getMileage() + " miles and " + primaryCar.getFuelLevel() + " gallons of gas.\n" ;
     }
                               
       if (secondaryCar != null){
      out += "Secondary car has " + secondaryCar.getMileage() + " miles and " + secondaryCar.getFuelLevel() + " gallons of gas.\n" ;
     }
       return out;
    }
   
    
    public static void main(String[] args){
      Fleet myFleet = new Fleet();
      System.out.println(myFleet);
      
      Car myCar = new Car();
      myCar.fillTank();
      myCar.setAverageMPG(Car.DEFAULT_MPG);
      myFleet.setPrimaryCar(myCar);
      
      Car yourCar = new Car();
      yourCar.fillTank();
      yourCar.setAverageMPG(Car.DEFAULT_MPG);
      myFleet.setSecondaryCar(yourCar);
      System.out.println(myFleet);
      
      myFleet.getPrimaryCar().drive(30);
      myFleet.getSecondaryCar().drive(30);
      myCar.drive(60);
      yourCar.drive(60);
      System.out.println(myFleet);
      
      myFleet.getPrimaryCar().fillTank();
      System.out.println(myFleet);
      myFleet.getPrimaryCar().drive(300);
      System.out.println(myFleet);
      myFleet.getSecondaryCar().drive(300);
      System.out.println(myFleet);
      
      Car companyCar = new Car ();
      companyCar.fillTank();
      companyCar.setAverageMPG(Car.DEFAULT_MPG);
      myFleet.setPrimaryCar(companyCar);
      System.out.println(myFleet);
      
      
      
            
    }
    
}


I need help with starting this assignments, I kind of have a basic idea of how arrays work... but I just don't know how to apply arrays in this assignment...

thanks in advance

Is this how it should look like?

  String [][] Cars = new String []


But where do i put PrimaryCar and SecondaryCar?

Is This A Good Question/Topic? 0
  • +

Replies To: Need help with Arrays

#2 fromTheSprawl  Icon User is offline

  • Bloodborne
  • member icon

Reputation: 522
  • View blog
  • Posts: 2,102
  • Joined: 28-December 10

Re: Need help with Arrays

Posted 23 October 2012 - 10:22 PM

I don't need to think you need a multiple dim array here, one should be enough. Basically, your prof wants you to get rid of any trace of primary and secondary car on your Fleet class, then add an add and remove method that will work with your declared array.

Also, declare your array as type of Car. When you iterate through the array you can access the car and its methods via the notation arrayName[index].

Quick example is:
car[0].drive();
car[2].fly();


Was This Post Helpful? 1
  • +
  • -

#3 realnoob  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 14-October 12

Re: Need help with Arrays

Posted 23 October 2012 - 10:31 PM

I'm getting all this kind of errors :online2long:

11 errors found:
File: J:\Computer Science\Fleet.java [line: 30]
Error: Variable must provide either dimension expressions or an array initializer
File: J:\Computer Science\Fleet.java [line: 37]
Error: primaryCar cannot be resolved to a variable
File: J:\Computer Science\Fleet.java [line: 39]
Error: secondaryCar cannot be resolved to a variable
File: J:\Computer Science\Fleet.java [line: 48]
Error: primaryCar cannot be resolved to a variable
File: J:\Computer Science\Fleet.java [line: 49]
Error: primaryCar cannot be resolved
File: J:\Computer Science\Fleet.java [line: 49]
Error: primaryCar cannot be resolved
File: J:\Computer Science\Fleet.java [line: 52]
Error: secondaryCar cannot be resolved to a variable
File: J:\Computer Science\Fleet.java [line: 53]
Error: secondaryCar cannot be resolved
File: J:\Computer Science\Fleet.java [line: 53]
Error: secondaryCar cannot be resolved
File: J:\Computer Science\Fleet.java [line: 64]
Error: Syntax error on token "[", Expression expected after this token
File: J:\Computer Science\Fleet.java [line: 65]
Error: Syntax error on token "[", Expression expected after this token

This post has been edited by realnoob: 23 October 2012 - 10:33 PM

Was This Post Helpful? 0
  • +
  • -

#4 fromTheSprawl  Icon User is offline

  • Bloodborne
  • member icon

Reputation: 522
  • View blog
  • Posts: 2,102
  • Joined: 28-December 10

Re: Need help with Arrays

Posted 23 October 2012 - 10:47 PM

Lol, where's the code you tried to run? ^^
Was This Post Helpful? 0
  • +
  • -

#5 realnoob  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 14-October 12

Re: Need help with Arrays

Posted 23 October 2012 - 10:51 PM

View PostfromTheSprawl, on 23 October 2012 - 10:47 PM, said:

Lol, where's the code you tried to run? ^^


I basically just did a "trial" run on this

I removed my instances and and their mutators and replaced it with an array but everything stayed the same except that i added [] to one of them

  String [] myCar = new String []; 

public int size(){
    int size = 0;
    if (primaryCar != null )
      size++;
    if (secondaryCar != null)
      size++;
    return size;
  }
  
   
  
   public String toString(){
     String out = "this fleet has " + size() + " cars. \n";
       if (primaryCar != null ){
      out += "Primary car has " + primaryCar.getMileage() + " miles and " + primaryCar.getFuelLevel() + " gallons of gas.\n" ;
     }
                               
       if (secondaryCar != null){
      out += "Secondary car has " + secondaryCar.getMileage() + " miles and " + secondaryCar.getFuelLevel() + " gallons of gas.\n" ;
     }
       return out;
    }
   
    
    public static void main(String[] args){
      Fleet myFleet = new Fleet();
      System.out.println(myFleet);

      // here where I added the array? []
      Car myCar = new Car();
      myCar[].fillTank();
      myCar[].setAverageMPG(Car.DEFAULT_MPG);
      myFleet.setPrimaryCar(myCar);
      
      Car yourCar = new Car();
      yourCar.fillTank();
      yourCar.setAverageMPG(Car.DEFAULT_MPG);
      myFleet.setSecondaryCar(yourCar);
      System.out.println(myFleet);
      
      myFleet.getPrimaryCar().drive(30);
      myFleet.getSecondaryCar().drive(30);
      myCar.drive(60);
      yourCar.drive(60);
      System.out.println(myFleet);
      
      myFleet.getPrimaryCar().fillTank();
      System.out.println(myFleet);
      myFleet.getPrimaryCar().drive(300);
      System.out.println(myFleet);
      myFleet.getSecondaryCar().drive(300);
      System.out.println(myFleet);
      
      Car companyCar = new Car ();
      companyCar.fillTank();
      companyCar.setAverageMPG(Car.DEFAULT_MPG);
      myFleet.setPrimaryCar(companyCar);
      System.out.println(myFleet);
      
      
      
            
    }
    
}

Was This Post Helpful? 0
  • +
  • -

#6 RozenKristal  Icon User is offline

  • D.I.C Regular

Reputation: 45
  • View blog
  • Posts: 381
  • Joined: 29-September 12

Re: Need help with Arrays

Posted 23 October 2012 - 10:56 PM

Ah man, first thing, your professor tell you to remove primary and secondary instances, yes you did, but you have to replace it with an array, if I am not wrong, that array have to have type Cars, so:

Cars[] myCar = new Cars[2];// use 2 because you replace primary and secondary, which is 2 different objects, which suggest 2 slots for the array.

Was This Post Helpful? 2
  • +
  • -

#7 fromTheSprawl  Icon User is offline

  • Bloodborne
  • member icon

Reputation: 522
  • View blog
  • Posts: 2,102
  • Joined: 28-December 10

Re: Need help with Arrays

Posted 23 October 2012 - 10:59 PM

Dude, I told you to remove all traces of primary and secondary Car, didn't I? RozenKristal's right. ^^
Was This Post Helpful? 0
  • +
  • -

#8 realnoob  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 14-October 12

Re: Need help with Arrays

Posted 23 October 2012 - 11:19 PM

hmmm... i'm still confused tho :helpsmilie:

so i removed everything like fromTheSprawl told me , and i added the line RozenKristal suggested... however i'm getting these two errors..

2 errors found:
File: J:\Computer Science\Fleet.java [line: 64]
Error: Syntax error on token "[", Expression expected after this token
File: J:\Computer Science\Fleet.java [line: 65]
Error: Syntax error on token "[", Expression expected after this token

public class Fleet {
  
  /*private Car primaryCar;
  private Car secondaryCar;
         
  public Car getPrimaryCar(){
    return primaryCar  ;
  }
  
  public void setPrimaryCar ( Car newCar) {
      primaryCar = newCar;
  }
    
  public Car getSecondaryCar(){
    return secondaryCar;
  }
  
  public void setSecondaryCar(Car newCar) {
    secondaryCar = newCar;
  }
 
   
  public int size(){
    int size = 0;
    if (primaryCar != null )
      size++;
    if (secondaryCar != null)
      size++;
    return size;
  }
  
   
  
   public String toString(){
     String out = "this fleet has " + size() + " cars. \n";
       if (primaryCar != null ){
      out += "Primary car has " + primaryCar.getMileage() + " miles and " + primaryCar.getFuelLevel() + " gallons of gas.\n" ;
     }
                               
       if (secondaryCar != null){
      out += "Secondary car has " + secondaryCar.getMileage() + " miles and " + secondaryCar.getFuelLevel() + " gallons of gas.\n" ;
     }
       return out;
    }*/
   
    
   
   Car[] myCar = new Car[2]; 
  //myCars[primaryCar].drive();
   
    public static void main(String[] args){
      Fleet myFleet = new Fleet();
      System.out.println(myFleet);
      
      Car myCar = new Car();
      myCar[].fillTank();
      myCar[].setAverageMPG(Car.DEFAULT_MPG);
      myFleet.setPrimaryCar(myCar);
      
      Car yourCar = new Car();
      yourCar.fillTank();
      yourCar.setAverageMPG(Car.DEFAULT_MPG);
      myFleet.setSecondaryCar(yourCar);
      System.out.println(myFleet);
      
      myFleet.getPrimaryCar().drive(30);
      myFleet.getSecondaryCar().drive(30);
      myCar.drive(60);
      yourCar.drive(60);
      System.out.println(myFleet);
      
      myFleet.getPrimaryCar().fillTank();
      System.out.println(myFleet);
      myFleet.getPrimaryCar().drive(300);
      System.out.println(myFleet);
      myFleet.getSecondaryCar().drive(300);
      System.out.println(myFleet);
      
      Car companyCar = new Car ();
      companyCar.fillTank();
      companyCar.setAverageMPG(Car.DEFAULT_MPG);
      myFleet.setPrimaryCar(companyCar);
      System.out.println(myFleet);
      
      
      
            
    }
    
}

how do i solve that problem?
I'm confused now is how do i get the outputs and stuff?

I really appreciate your guys patience with me... PLEASE bare with me
Was This Post Helpful? 0
  • +
  • -

#9 RozenKristal  Icon User is offline

  • D.I.C Regular

Reputation: 45
  • View blog
  • Posts: 381
  • Joined: 29-September 12

Re: Need help with Arrays

Posted 23 October 2012 - 11:27 PM

First of all, you cant just:
System.out.println(myFleet);
like that...myFleet is an object of type Fleet, it is not a primitive type, so that line wont work. Second, why you are creating myCar as type Car again? We decided that it should be an array, which you declared already. I think you need to read some more about the concepts which you covered in class, cuz at this rate, it is hard to solve your assignment if you dont understand these.
Was This Post Helpful? 0
  • +
  • -

#10 realnoob  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 14-October 12

Re: Need help with Arrays

Posted 23 October 2012 - 11:32 PM

View PostRozenKristal, on 23 October 2012 - 11:27 PM, said:

First of all, you cant just:
System.out.println(myFleet);
like that...myFleet is an object of type Fleet, it is not a primitive type, so that line wont work. Second, why you are creating myCar as type Car again? We decided that it should be an array, which you declared already. I think you need to read some more about the concepts which you covered in class, cuz at this rate, it is hard to solve your assignment if you dont understand these.


I dont even know what I had that there... thanks..

well I read the chapter but i still dont get it... and the problem is i'm more like a hands on learning person... and when my teacher did an example of arrays he did simple things and then the homework it's way over the top... like he taught 1+1 in class and the homework was solve integrals and derivatives...

I'm screwed :sad2: :(
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1