Welcome to Dream.In.Code
Become a Java Expert!

Join 150,195 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,998 people online right now. Registration is fast and FREE... Join Now!




Vehicle assignment..

 
Reply to this topicStart new topic

Vehicle assignment.., New post..stilling working on this one

LadyWolf
24 Sep, 2008 - 09:46 PM
Post #1

D.I.C Head
**

Joined: 25 Jun, 2008
Posts: 168


My Contributions
It's was starting to get confusing in the other post. Here's what I have at the moment. I'm basically working on VehicleTest at this time..I know I need more in Vehicle for computing tank milage between the vehicles, but I would like to get VehicleTest working using JOptionPane.

CODE


import javax.swing.*;
class Vehicle
{

   private String vinNumber;
   private String make;
   private String model;
   private int year;
   private double price;
   private double milesPerGallon;
   private double tanksize;


      public Vehicle(String vinNumber, String make, String model, int year, double price, double milesPerGallon, double tanksize)
      {
         this.vinNumber = vinNumber;
         this.make = make;
         this.model = model;
         this.year = year;
         this.price = price;
         this.milesPerGallon = milesPerGallon;
         this.tanksize = tanksize;

         System.out.println("Creates an vehicle information list\n");

      }

      public Vehicle(String vinNumber, String make, String model, int year)
      {
         this.vinNumber = vinNumber;
         this.make = make;
         this.model = model;
         this.year = year;

         System.out.println("Creates an vehicle information list\n");

      }

      public void vehicleInfo()
      {
         System.out.println(" vinNumber = " + vinNumber +
         "\n make = " + make + "\n model = " + model + "\n year = "
         + year + "\n price = " + price + "\n milespergallon = "
         + milesPerGallon + "\n tanksize = " + tanksize + "\n distance = " + computeTankMilage());
      }

      public void setMilesPerGallon(double m)
          {
                 milesPerGallon = m;
        }

      public void setTanksize(double t)
        {
              tanksize = t;
        }

      public void setPrice(double d)
        {
           price = d;
           }

      public String getVinNumber()
       {
         return vinNumber;
       }

       public String getMake()
       {
         return make;
       }

       public String getModel()
       {
         return model;
       }

       public int getYear()
       {
         return year;
       }

       public double getPrice()
       {
         return price;
       }

       public double getMilesPerGallon()
       {
         return milesPerGallon;
       }

       public double getTankSize()
       {
         return tanksize;
       }


CODE


  import javax.swing.*;
  class VehicleTest
  {

     public static void main(String[] args)
     {
         //create 3 vehicles
         Vehicle v1 = new Vehicle("52348635", "Chevrolet", "TrailBlazer", 2005, 15995.00, 25, 17);
         v1.vehicleInfo();
         Vehicle v2 = new Vehicle("56352354", "Pontiac", "Sunfire", 2004);

         Vehicle v3 = new Vehicle("36488277", "Chevrolet", "Cavalier", 2005);

User is offlineProfile CardPM
+Quote Post

LadyWolf
RE: Vehicle Assignment..
25 Sep, 2008 - 09:29 AM
Post #2

D.I.C Head
**

Joined: 25 Jun, 2008
Posts: 168


My Contributions
I think this finishes Vehicle class, as far as computing the vehicles goes...Not sure..

CODE


import javax.swing.*;
class Vehicle
{

   private String vinNumber;
   private String make;
   private String model;
   private int year;
   private double price;
   private double milesPerGallon;
   private double tanksize;


      public Vehicle(String vinNumber, String make, String model, int year, double price, double milesPerGallon, double tanksize)
      {
         this.vinNumber = vinNumber;
         this.make = make;
         this.model = model;
         this.year = year;
         this.price = price;
         this.milesPerGallon = milesPerGallon;
         this.tanksize = tanksize;

         System.out.println("Creates an vehicle information list\n");

      }

      public Vehicle(String vinNumber, String make, String model, int year)
      {
         this.vinNumber = vinNumber;
         this.make = make;
         this.model = model;
         this.year = year;

         System.out.println("Creates an vehicle information list\n");

      }

      public void vehicleInfo()
      {
         System.out.println(" vinNumber = " + vinNumber +
         "\n make = " + make + "\n model = " + model + "\n year = "
         + year + "\n price = " + price + "\n milespergallon = "
         + milesPerGallon + "\n tanksize = " + tanksize + "\n distance = " + computeTankMilage());
      }

      public void setMilesPerGallon(double m)
          {
                 milesPerGallon = m;
        }

      public void setTanksize(double t)
        {
              tanksize = t;
        }

      public void setPrice(double d)
        {
           price = d;
           }

      public String getVinNumber()
       {
         return vinNumber;
       }

       public String getMake()
       {
         return make;
       }

       public String getModel()
       {
         return model;
       }

       public int getYear()
       {
         return year;
       }

       public double getPrice()
       {
         return price;
       }

       public double getMilesPerGallon()
       {
         return milesPerGallon;
       }

       public double getTankSize()
       {
         return tanksize;
       }

      public double computeTankMilage()
       {
           return milesPerGallon * tanksize;
       }
       public Vehicle compareVehicle(Vehicle other)
       {
            if (this.computeTankMilage() > other.computeTankMilage() )
                return this;
            else
                return other;
       }

}


This post has been edited by LadyWolf: 25 Sep, 2008 - 09:29 AM
User is offlineProfile CardPM
+Quote Post

bbq
RE: Vehicle Assignment..
25 Sep, 2008 - 09:51 AM
Post #3

D.I.C Head
Group Icon

Joined: 15 May, 2008
Posts: 200



Thanked: 17 times
Dream Kudos: 50
My Contributions
Just on this method
java

public Vehicle compareVehicle(Vehicle other)
{
if (this.computeTankMilage() > other.computeTankMilage() )
return this;
else
return other;
}


You will actually need to parse two vehicles in order to compare them.
java
public Vehicle compareVehicle(Vehicle aVehicle, Vehicle bVehicle)

if(aVehicle.computeTankMilage() > bVehicle.computeTankMilage())
return aVehicle
else
return bVehicle


That might not work btw, but you definately need to parse 2 vehicles in order to compare them confused.gif

This post has been edited by bbq: 25 Sep, 2008 - 09:52 AM
User is offlineProfile CardPM
+Quote Post

LadyWolf
RE: Vehicle Assignment..
25 Sep, 2008 - 02:01 PM
Post #4

D.I.C Head
**

Joined: 25 Jun, 2008
Posts: 168


My Contributions
QUOTE(bbq @ 25 Sep, 2008 - 01:51 PM) *

Just on this method
java

public Vehicle compareVehicle(Vehicle other)
{
if (this.computeTankMilage() > other.computeTankMilage() )
return this;
else
return other;
}


You will actually need to parse two vehicles in order to compare them.
java
public Vehicle compareVehicle(Vehicle aVehicle, Vehicle bVehicle)

if(aVehicle.computeTankMilage() > bVehicle.computeTankMilage())
return aVehicle
else
return bVehicle


That might not work btw, but you definately need to parse 2 vehicles in order to compare them confused.gif


I'm working on parsing them together in the VehicleTest class. That would be where it needs to be done, right?
User is offlineProfile CardPM
+Quote Post

pbl
RE: Vehicle Assignment..
25 Sep, 2008 - 02:55 PM
Post #5

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
QUOTE(bbq @ 25 Sep, 2008 - 10:51 AM) *

Just on this method
java

public Vehicle compareVehicle(Vehicle other)
{
if (this.computeTankMilage() > other.computeTankMilage() )
return this;
else
return other;
}


You will actually need to parse two vehicles in order to compare them.
java
public Vehicle compareVehicle(Vehicle aVehicle, Vehicle bVehicle)

if(aVehicle.computeTankMilage() > bVehicle.computeTankMilage())
return aVehicle
else
return bVehicle


Yes the method is comparing 2 vehicules:

this.computeTankMillage() and other.computeTankMillage()


bbq I guess you looked too fast at that one biggrin.gif

nothing wrong with LadyWolf's code. Nothing wrong using "this".
This is what the compareTo() method does in all classes that implement the Comparable interface.

CODE

class Number {
   int n;
....

   Number getGreatest(Number x) {
      if(this.n > x.n)
        return this;
      else
         return x;
    }
}


QUOTE

That might not work btw, but you definately need to parse 2 vehicles in order to compare them confused.gif


That is what the method is actually doing

This post has been edited by pbl: 25 Sep, 2008 - 02:59 PM
User is offlineProfile CardPM
+Quote Post

pbl
RE: Vehicle Assignment..
25 Sep, 2008 - 03:04 PM
Post #6

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
LadyWolf there was a glitch in your cut & paste of your Testvehicule class
The code is not complete. We don't see where to use the OptionPane

User is offlineProfile CardPM
+Quote Post

bbq
RE: Vehicle Assignment..
25 Sep, 2008 - 07:54 PM
Post #7

D.I.C Head
Group Icon

Joined: 15 May, 2008
Posts: 200



Thanked: 17 times
Dream Kudos: 50
My Contributions
QUOTE(LadyWolf @ 25 Sep, 2008 - 03:01 PM) *

QUOTE(bbq @ 25 Sep, 2008 - 01:51 PM) *

Just on this method
java

public Vehicle compareVehicle(Vehicle other)
{
if (this.computeTankMilage() > other.computeTankMilage() )
return this;
else
return other;
}


You will actually need to parse two vehicles in order to compare them.
java
public Vehicle compareVehicle(Vehicle aVehicle, Vehicle bVehicle)

if(aVehicle.computeTankMilage() > bVehicle.computeTankMilage())
return aVehicle
else
return bVehicle


That might not work btw, but you definately need to parse 2 vehicles in order to compare them confused.gif


I'm working on parsing them together in the VehicleTest class. That would be where it needs to be done, right?



Oh my that is a complete lapse of concentration on my behalf. Disregard my above post completely lol, sorry LadyWolf and thanks to pbl for checking over it
User is offlineProfile CardPM
+Quote Post

pbl
RE: Vehicle Assignment..
25 Sep, 2008 - 08:06 PM
Post #8

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
QUOTE(bbq @ 25 Sep, 2008 - 08:54 PM) *

Oh my that is a complete lapse of concentration on my behalf. Disregard my above post completely lol, sorry LadyWolf and thanks to pbl for checking over it

It was by accident... I was expecting something else on that post (the complete code for the Test class) I usually don't bother checking posts already answered by bbq

User is offlineProfile CardPM
+Quote Post

LadyWolf
RE: Vehicle Assignment..
26 Sep, 2008 - 08:51 AM
Post #9

D.I.C Head
**

Joined: 25 Jun, 2008
Posts: 168


My Contributions
Sorry pbl I was still working Vehicle, and only posted what I had at that time in VehicleTest. Here's what I have at the moment, but I'm still not sure if I did this right using JOptionPane....This does run though biggrin.gif Also, I was wondering if I could use JOptionPane instead of println for the output part of VehicleTest? I've been trying to work it to do this, but with no success, it's probably something I'm not doing right.

CODE


import javax.swing.*;
class Vehicle
{

   private String vinNumber;
   private String make;
   private String model;
   private int year;
   private double price;
   private double milesPerGallon;
   private double tanksize;


      public Vehicle(String vinNumber, String make, String model, int year, double price, double milesPerGallon, double tanksize)
      {
         this.vinNumber = vinNumber;
         this.make = make;
         this.model = model;
         this.year = year;
         this.price = price;
         this.milesPerGallon = milesPerGallon;
         this.tanksize = tanksize;

         System.out.println("Creates an vehicle information list\n");

      }

      public Vehicle(String vinNumber, String make, String model, int year)
      {
         this.vinNumber = vinNumber;
         this.make = make;
         this.model = model;
         this.year = year;

         System.out.println("Creates an vehicle information list\n");

      }

      public void vehicleInfo()
      {
         System.out.println(" vinNumber = " + vinNumber +
         "\n make = " + make + "\n model = " + model + "\n year = "
         + year + "\n price = " + price + "\n milespergallon = "
         + milesPerGallon + "\n tanksize = " + tanksize + "\n distance = " + computeTankMilage());
      }

      public void setMilesPerGallon(double m)
          {
                 milesPerGallon = m;
        }

      public void setTanksize(double t)
        {
              tanksize = t;
        }

      public void setPrice(double d)
        {
           price = d;
           }

      public String getVinNumber()
       {
         return vinNumber;
       }

       public String getMake()
       {
         return make;
       }

       public String getModel()
       {
         return model;
       }

       public int getYear()
       {
         return year;
       }

       public double getPrice()
       {
         return price;
       }

       public double getMilesPerGallon()
       {
         return milesPerGallon;
       }

       public double getTankSize()
       {
         return tanksize;
       }

      public double computeTankMilage()
       {
           return milesPerGallon * tanksize;
       }
       public Vehicle compareVehicle(Vehicle other)
       {
            if (this.computeTankMilage() > other.computeTankMilage() )
                return this;
            else
                return other;
       }

}


CODE


  import javax.swing.*;
  class VehicleTest
  {

     public static void main(String[] args)
     {
         //create 3 vehicles
         Vehicle v1 = new Vehicle("52348635", "Chevrolet", "TrailBlazer", 2005, 15995.00, 25, 17);
         v1.vehicleInfo();
         Vehicle v2 = new Vehicle("56352354", "Pontiac", "Sunfire", 2004);

         Vehicle v3 = new Vehicle("36488277", "Chevrolet", "Cavalier", 2005);

         v2.setPrice(Double.parseDouble(JOptionPane.showInputDialog("Enter price")));
         v2.setMilesPerGallon(Double.parseDouble(JOptionPane.showInputDialog("Enter MPG")));
         v2.setTanksize(Double.parseDouble(JOptionPane.showInputDialog("Enter tank size")));
         v3.setPrice(Double.parseDouble(JOptionPane.showInputDialog("Enter price")));
         v3.setMilesPerGallon(Double.parseDouble(JOptionPane.showInputDialog("Enter MPG")));
         v3.setTanksize(Double.parseDouble(JOptionPane.showInputDialog("Enter tank size")));

         v2.vehicleInfo();
         v3.vehicleInfo();


           System.out.println("\n\n\nThe car that can go fartherest is:\n");

         Vehicle max = v2.compareVehicle(v3);
         max.vehicleInfo();
     }

   }


This post has been edited by LadyWolf: 26 Sep, 2008 - 09:27 AM
User is offlineProfile CardPM
+Quote Post

pbl
RE: Vehicle Assignment..
26 Sep, 2008 - 01:40 PM
Post #10

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
OK it works...
So what is the question ?
User is offlineProfile CardPM
+Quote Post

LadyWolf
RE: Vehicle Assignment..
26 Sep, 2008 - 02:23 PM
Post #11

D.I.C Head
**

Joined: 25 Jun, 2008
Posts: 168


My Contributions
I was wondering if there was a way use JOptionPane instead of println for the output? I've been trying to work it to do this, but with no success, it's probably something I'm not doing right. Also I noticed that when running it that the output doesn't have a space in between the 2 vehicles information that are being compared. I've tried inserting \n in different parts of the code, but it's not working the way I want it to..
User is offlineProfile CardPM
+Quote Post

LadyWolf
RE: Vehicle Assignment..
26 Sep, 2008 - 03:44 PM
Post #12

D.I.C Head
**

Joined: 25 Jun, 2008
Posts: 168


My Contributions
QUOTE(LadyWolf @ 26 Sep, 2008 - 06:23 PM) *

I was wondering if there was a way use JOptionPane instead of println for the output? I've been trying to work it to do this, but with no success, it's probably something I'm not doing right. Also I noticed that when running it that the output doesn't have a space in between the 2 vehicles information that are being compared. I've tried inserting \n in different parts of the code, but it's not working the way I want it to..


I figured out how to get the spaces where I need them biggrin.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 04:34AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads