Having trouble understanding methods and classes

  • (2 Pages)
  • +
  • 1
  • 2

29 Replies - 1138 Views - Last Post: 15 October 2012 - 09:42 PM Rate Topic: -----

#1 realnoob  Icon User is offline

  • New D.I.C Head

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

Having trouble understanding methods and classes

Posted 14 October 2012 - 06:25 PM

Hello:

I'm struggling with methods and classes, my teacher is really terrible at explaining and I went to his office hours, but still he does not make sense to me at all, so far I have been teaching myself from the text book. Unfortunately, the book does not explain methods and classes very well (at least i'm not understanding what the book is saying). So we had two assignments about methods and classes, the first one i think i have done it correctly, the second assignment is continuation for the first assignment.

I had to ask for help to do this assignment and the help i received was just with coding, I just couldn't understand the this topic very well :(
this what the first assignment asked me to do
1.Create a java class called Car. Implement a main method inside this class. This method will only be used for your own testing; I will use my own main method when I grade your programs.

2.A car has an odometer (double), an amount of fuel in its gas tank (double; number of gallons), an average miles-per-gallon (double), a mileage at which the oil was last changed (double), and a serial number (int). Declare member variables for these and implement the following accessors and mutators (Note: Do not implement a mutator if a mutator is not listed. This will likely cause your class not to work with my test program.):
getMileage()
getFuelLevel()
getAverageMPG(), setAverageMPG()
getLastOilChange()
getSerialNumber(), setSerialNumber()

3.Implement methods with the following behaviors:

a)void drive(double miles) drive the car for the specified number of miles. This increases the odometer reading and decreases the fuel level by the number of miles divided by the MPG. If there is not enough fuel in the tank, this should print an error and return. If the number of miles since the last oil change (after driving the specified amount) is over 3000, print a message indicating that the car needs its oil changed. If has been more than 4500 miles, indicate that it is urgent that the car have its oil changed.

b)double fillTank() This car has a 15-gallon tank. Set the tank level to full (15 gallons) and return the amount of fuel that was required to fill the tank.

C)void changeOil() Set the last oil change mileage to the the current value of the odometer.

and this is what i did


public class Car {

    private double odometer;
    private double numberOfGallons;
    private double averageMPG;
    private double lastOilChange;
    private int serialNumber;
    

    
    public Car()
    {
      odometer = 0.0;
      numberOfGallons = 15.0;
      averageMPG = 20.0;
      lastOilChange = 0.0;
      serialNumber = 0;
      
    }
    
    public double getMileage(){
      return odometer;
    }
    
    public double getFuelLevel() {
      return numberOfGallons;
    }
    
    public double getAverageMPG() {
      return averageMPG;
    } 
    public void setAverageMPG( double newmpg){ 
      averageMPG = newmpg;
    }
    
    public double getLastOilChange() {
      return lastOilChange;
    }
    
    public int getSerialNumber() {
      return serialNumber;
    }
    public void setSerialNumber ( int newserial) {
      serialNumber = newserial;
    }
    public void drive ( double miles) {
      odometer = odometer + miles; 
      numberOfGallons = numberOfGallons - (miles / averageMPG);
      if ( numberOfGallons <= 0 ) 
        System.out.println("Error not enough fuel");
      double milesSinceLastOilChange = miles - lastOilChange;
      if (milesSinceLastOilChange >= 3000)
        System.out.println("Car needs its oil changed");
      if (milesSinceLastOilChange >= 4500)
        System.out.println("Urgent the car must have its oil changed");
    }
      
    public double fillTank( ){
      double amountToFill = 15 - numberOfGallons; 
      numberOfGallons = 15;
      return amountToFill;
      
    }
    
    public void changeOil(){
      lastOilChange = odometer;
         
    }
      
    public static void main(String[] args)
    {
      Car yossfsCar = new Car();
      yossfsCar.drive(300);
      yossfsCar.fillTank();
      yossfsCar.changeOil();
           
    }
        
  }


now the second assignment is titled Modular Code (i don't even know what that is :()

1.In this assignment, we will improve upon the result of Assignment 6 and develop a very simple fleet management system. This fleet is very small; there is a primary car, a secondary car, and either or both of these slots may be open.

2.Add a named constant to the car class called DEFAULT_MPG to represent a default MPG of 23.0

3.Create a Java class called fleet. Implement a main mathod in this class. As before, this is for your own testing; I will use my own main method to grade your program. This fleet class should have two instance variables of type car, primaryCar and secondaryCar.Implement accessors and mutators for these properties.

4.Implement an instance method in the fleet class called size that returns the number of cars (0, 1, or 2) in the fleet.
5.Also implement an instance method in the fleet class called toString. The toString method should take no arguments and return a String containing the size of the fleet and the mileage and fuel level of any cars that are in the fleet.

6.In your main method, create a fleet, print it out, add a primary and secondary car, and again get (and print) the description. Drive each car 30 miles, then 60 miles. Fuel up the primary car and then attempt to drive each 300 miles. Print out the fleet's description after each step. Then add a new primary car and print the fleet. When you create each car, immediately fill its gas tank and set the average miles per gallon to the value in the DEFAULT_MPG named constant.

Now Am I supposed to have a new public class Fleet in my first program assignment ? I tried that and i get error code : The public type Fleet must be defined in its own file
How is it going to be in its own file, while this program needs the car program? I'm confused please help.....

oopps how do i edit my thread i forgot something

Is This A Good Question/Topic? 0
  • +

Replies To: Having trouble understanding methods and classes

#2 RozenKristal  Icon User is offline

  • D.I.C Regular

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

Re: Having trouble understanding methods and classes

Posted 14 October 2012 - 06:32 PM

That mean create a different file that will have

public class Fleet
{
   Stuffs in here
}


The car class has methods (which you use to do something and get a result out of it). So while you have a Fleet class, in the fleet class, you can make an object that belong to the car class (this object reside in the fleet class), this object can call the methods of the car class and do stuffs.
Was This Post Helpful? 0
  • +
  • -

#3 realnoob  Icon User is offline

  • New D.I.C Head

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

Re: Having trouble understanding methods and classes

Posted 14 October 2012 - 06:36 PM

View PostRozenKristal, on 14 October 2012 - 06:32 PM, said:

That mean create a different file that will have

public class Fleet
{
   Stuffs in here
}


The car class has methods (which you use to do something and get a result out of it). So while you have a Fleet class, in the fleet class, you can make an object that belong to the car class (this object reside in the fleet class), this object can call the methods of the car class and do stuffs.



So the Fleet program will never use the Car program which i had? The part i'm confused about is when the teacher said Improve the car program and Add a named constant to the car class called DEFAULT_MPG to represent a default MPG of 23.0.

The problem is i don't understand all of that or how methods and classes work
Was This Post Helpful? 0
  • +
  • -

#4 RozenKristal  Icon User is offline

  • D.I.C Regular

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

Re: Having trouble understanding methods and classes

Posted 14 October 2012 - 06:43 PM

you can add this in ur car class as a class field:
private final double DEFAULT_MPG =23.0;

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: Having trouble understanding methods and classes

Posted 14 October 2012 - 06:54 PM

View PostRozenKristal, on 14 October 2012 - 06:43 PM, said:

you can add this in ur car class as a class field:
private final double DEFAULT_MPG =23.0;


I'm sorry but i'm totally lost, if you would please explain the step you just told me to do... so i can understand the concept.
Was This Post Helpful? 0
  • +
  • -

#6 RozenKristal  Icon User is offline

  • D.I.C Regular

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

Re: Having trouble understanding methods and classes

Posted 14 October 2012 - 06:59 PM

Quote

1.In this assignment, we will improve upon the result of Assignment 6 and develop a very simple fleet management system. This fleet is very small; there is a primary car, a secondary car, and either or both of these slots may be open.

2.Add a named constant to the car class called DEFAULT_MPG to represent a default MPG of 23.0

3.Create a Java class called fleet. Implement a main mathod in this class. As before, this is for your own testing; I will use my own main method to grade your program. This fleet class should have two instance variables of type car, primaryCar and secondaryCar.Implement accessors and mutators for these properties.


So at 2) it tells u to add a class field to the car class and the value is 23.0

at 3) You make a class called fleet that is in a different file, it should have a main method to test what you going to write in here. In this class, you have two more class fields that of type Car, and the name would be primaryCar and secondaryCar. Then you make set and get methods for both.
Was This Post Helpful? 0
  • +
  • -

#7 realnoob  Icon User is offline

  • New D.I.C Head

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

Re: Having trouble understanding methods and classes

Posted 14 October 2012 - 07:04 PM

View PostRozenKristal, on 14 October 2012 - 06:59 PM, said:

Quote

1.In this assignment, we will improve upon the result of Assignment 6 and develop a very simple fleet management system. This fleet is very small; there is a primary car, a secondary car, and either or both of these slots may be open.

2.Add a named constant to the car class called DEFAULT_MPG to represent a default MPG of 23.0

3.Create a Java class called fleet. Implement a main mathod in this class. As before, this is for your own testing; I will use my own main method to grade your program. This fleet class should have two instance variables of type car, primaryCar and secondaryCar.Implement accessors and mutators for these properties.


So at 2) it tells u to add a class field to the car class and the value is 23.0

at 3) You make a class called fleet that is in a different file, it should have a main method to test what you going to write in here. In this class, you have two more class fields that of type Car, and the name would be primaryCar and secondaryCar. Then you make set and get methods for both.


So i made the fleet class in a total different file (its own program), but like i still dont get how will fleet and car program will work together, since they are separate, my teacher never went over how these things work...
public class Fleet {
  private Car primaryCar;
  private Car secondaryCar;
  private int size; 
  private String description; 
  
  public Car getPrimaryCar(){
    return primaryCar;
  }
  
  public Car getSecondaryCar(){
    return secondaryCar;
  }

does that look right?
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

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

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

Re: Having trouble understanding methods and classes

Posted 14 October 2012 - 07:06 PM

Good
Was This Post Helpful? 0
  • +
  • -

#9 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

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

Re: Having trouble understanding methods and classes

Posted 14 October 2012 - 07:08 PM

Yep, that looks correct. Now on your main method, set them and invoke them.
public static void main(String[] args){
Car bmw = new Car();
bmw.setStuff();
//set more values as needed, or if you have a constructor, use that, then...
setPrimaryCar(bmw);
}


Something just like that.

This post has been edited by fromTheSprawl: 14 October 2012 - 07:27 PM

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: Having trouble understanding methods and classes

Posted 14 October 2012 - 07:18 PM

View PostfromTheSprawl, on 14 October 2012 - 07:08 PM, said:

Yep, that looks correct. Now on your main method, set them and invoke them.
public static void main(String[] args){
Car bmw = new Car();
car.setStuff();
//set more values as needed, or if you have a constructor, use that, then...
setPrimaryCar(bmw);
}


Something just like that.


I'm getting this error
Error: car cannot be resolved
Was This Post Helpful? 0
  • +
  • -

#11 RozenKristal  Icon User is offline

  • D.I.C Regular

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

Re: Having trouble understanding methods and classes

Posted 14 October 2012 - 07:21 PM

View Postrealnoob, on 14 October 2012 - 07:18 PM, said:

View PostfromTheSprawl, on 14 October 2012 - 07:08 PM, said:

Yep, that looks correct. Now on your main method, set them and invoke them.
public static void main(String[] args){
Car bmw = new Car();
car.setStuff();
//set more values as needed, or if you have a constructor, use that, then...
setPrimaryCar(bmw);
}


Something just like that.


I'm getting this error
Error: car cannot be resolved



That is an example... dont put that in ur code.
Was This Post Helpful? 1
  • +
  • -

#12 realnoob  Icon User is offline

  • New D.I.C Head

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

Re: Having trouble understanding methods and classes

Posted 14 October 2012 - 07:27 PM

:(

I'm still lost.... my problem is not understanding methods and classes and how they function... in addition to how different programs work together :(
Was This Post Helpful? 0
  • +
  • -

#13 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

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

Re: Having trouble understanding methods and classes

Posted 14 October 2012 - 07:27 PM

Whoops, my bad. I used car when I should have used bmw. There, fixed it. Yeah, RozenKristal's right. ^^
Was This Post Helpful? 0
  • +
  • -

#14 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

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

Re: Having trouble understanding methods and classes

Posted 14 October 2012 - 07:34 PM

Your problem probably stems from a lack of understanding of object oriented principles. Think of classes as an object. So, a Car is an object, a Fleet is too. Now, a Fleet can contain Cars(according to your professor! How dare he/she! lol).

Basically, Fleet should contain two Cars according to your prof. To invoke an object, simply declare and initialize it on where you want it to be, say, for your example, in the main method of Fleet:
public class Fleet{
public static void main(String[] args){
Car primaryCar = new Car();//We have our primary car!
Car secondaryCar = new Car();//We have our secondary car!
}
}



Now, you have setters and getters right? Use that to put them inside the Fleet class:
setPrimaryCar(primaryCar);



Since all Cars have methods you've made on your previous assignment, you can invoke them as needed, before you set the cars for Fleet:
Car primaryCar = new Car();
primaryCar.setGasoline(10000000000);
primaryCar.setDriver("Angelina Jolie");
setPrimaryCar(primaryCar);


You can even do it after you set the cars!
Car primaryCar = new Car();
setPrimaryCar(primaryCar);
getPrimaryCar().setGasoline(-1010101010);
getPrimaryCar().setDriver("Brad Pitt");


See? All of this stuff is done within the comforts of the Fleet's main method. ^^
Was This Post Helpful? 0
  • +
  • -

#15 RozenKristal  Icon User is offline

  • D.I.C Regular

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

Re: Having trouble understanding methods and classes

Posted 14 October 2012 - 07:41 PM

View PostfromTheSprawl, on 14 October 2012 - 07:34 PM, said:

Your problem probably stems from a lack of understanding of object oriented principles. Think of classes as an object. So, a Car is an object, a Fleet is too. Now, a Fleet can contain Cars(according to your professor! How dare he/she! lol).

Basically, Fleet should contain two Cars according to your prof. To invoke an object, simply declare and initialize it on where you want it to be, say, for your example, in the main method of Fleet:
public class Fleet{
public static void main(String[] args){
Car primaryCar = new Car();//We have our primary car!
Car secondaryCar = new Car();//We have our secondary car!
}
}



Now, you have setters and getters right? Use that to put them inside the Fleet class:
setPrimaryCar(primaryCar);



Since all Cars have methods you've made on your previous assignment, you can invoke them as needed, before you set the cars for Fleet:
Car primaryCar = new Car();
primaryCar.setGasoline(10000000000);
primaryCar.setDriver("Angelina Jolie");
setPrimaryCar(primaryCar);


You can even do it after you set the cars!
Car primaryCar = new Car();
setPrimaryCar(primaryCar);
getPrimaryCar().setGasoline(-1010101010);
getPrimaryCar().setDriver("Brad Pitt");


See? All of this stuff is done within the comforts of the Fleet's main method. ^^


You gave him excellent examples, bravo, but I think this part may be a little confusing to him.

setPrimaryCar(primaryCar);


setPrimaryCar is a setter method in fleet class, while primaryCar is the object you just created.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2