Click here to Skip to main content
16,005,181 members
Home / Discussions / Java
   

Java

 
AnswerRe: how to transfer PDF file from servlet on server to client Pin
bennis989-Oct-11 16:13
bennis989-Oct-11 16:13 
GeneralRe: how to transfer PDF file from servlet on server to client Pin
williamroma10-Oct-11 10:24
williamroma10-Oct-11 10:24 
GeneralRe: how to transfer PDF file from servlet on server to client Pin
bennis9810-Oct-11 18:09
bennis9810-Oct-11 18:09 
GeneralRe: how to transfer PDF file from servlet on server to client Pin
williamroma11-Oct-11 5:20
williamroma11-Oct-11 5:20 
GeneralRe: how to transfer PDF file from servlet on server to client Pin
bennis9811-Oct-11 20:47
bennis9811-Oct-11 20:47 
GeneralRe: how to transfer PDF file from servlet on server to client Pin
David Skelly11-Oct-11 22:14
David Skelly11-Oct-11 22:14 
GeneralRe: how to transfer PDF file from servlet on server to client Pin
bennis9812-Oct-11 16:19
bennis9812-Oct-11 16:19 
Questionfor loop to find the lowest price in the array Pin
mastdesi7-Oct-11 13:23
mastdesi7-Oct-11 13:23 
Original question from assignment:
3. Write a driver program (where the main() method is) that would utilize all of your classes. The driver class can be in a separate package or in any of the already existing four packages. In the main() method you must:
a. Create various objects from the 6 classes, and display all their information using the toString() method;
b. Test the equality of some to the created objects using the equals() method;
c. Create an array of 10 Vehicule objects and fill that array with various objects from the 6 classes (each class must have at least one entry in that array);
d. Trace that array to find the object that has the cheapest price. Display all information of that object along with its location (index) in the array.
--------------------------------

I can figure out how to use a for loop to figure out the lowest price in the array. We were told to use the accessor method to get the price but how do i get the lowest price.

I have a total of 6 class. but these 2 should be enough to get the idea. and the driver.

Driver:
Java
import java.util.ArrayList;

import Package1.Vehicule;
import Package2.Car;
import Package2.FamilyCar;
import Package2.FamilyCar.CarType;
import Package2.SportsCar;
import Package3.Truck;
import Package3.Truck.TruckType;
import Package4.Bus;
import Package4.Bus.BusType;


public class Driver {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		CarType ct = null;
		TruckType tt = null;
		BusType bt = null;
		
		Vehicule vTest = new Vehicule("Hybrid", 1.9, 12000);
		System.out.println(vTest);
		
		Vehicule vvTest = new Vehicule("Hybrid", 1.9, 12000);
		System.out.println(vvTest);
		
		Car cTest = new Car("Honda", "Civic", 2009, "Unleaded", 2.8, 13000);
		System.out.println(cTest);
		
		FamilyCar fTest = new FamilyCar(7, ct.Sedan, "Honda", "Odessey", 
				2009, "Unleaded", 2.8, 13000);
		System.out.println(fTest);
		
		SportsCar sTest = new SportsCar(false, 250, "Mazda", "Rx8", 2004,
				"Unleaded", 1.5, 15000);
		System.out.println(sTest);
		
		Truck tTest = new Truck(tt.Tank, "Diesel", 1.2, 10000);
		System.out.println(tTest);
		
		Bus bTest = new Bus(bt.city, "Hybrid", 1.9, 12000);
		System.out.println(bTest);
		
		// Testing equals
		System.out.println(vTest.equals(vvTest));
		System.out.println(vTest.equals(bTest));
		
		Vehicule[] vehicules = new Vehicule[10];
		vehicules[0] = cTest;
		vehicules[1] = vTest;
		vehicules[2] = tTest;
		vehicules[3] = cTest;
		vehicules[4] = cTest;
		vehicules[5] = cTest;
		vehicules[6] = cTest;
		vehicules[7] = cTest;
		vehicules[8] = bTest;
		vehicules[9] = cTest;
		

		int fin = 0;
		int ii = 1;
		for (int i = 0; i<10; i++, ii++)
		{
			for (;vehicules[i].getPrice() < vehicules[ii].getPrice();)
				fin = i;

		}

	}

}



Vehicule class:
Java
package Package1;

public class Vehicule {
	protected String fuelT;
	protected double consum;
	protected int price;
	
	public Vehicule ()
	{
		fuelT = "";
		consum = 0.0;
		price = 0;
	}
	
	public Vehicule(String f, double c, int p)
	{
		this.fuelT = f;
		this.consum = c;
		this.price = p;
	}
	
	public String toString()
	{
		return "This Vehicule is a " + fuelT 
		+ ", consumes " + consum + "liters/100km and costs $" 
		+ price + ".";
	}
	
	// accessors
    public String getFuelT() {
        return this.fuelT;
    }
 
    public double getConsum() {
        return this.consum;
    }
 
    public int getPrice() {
        return this.price;
    }
    
    // mutators
    public void setFuelT(String f) {
        this.fuelT = f;
    }
 
    public void setConsum(double c) {
        this.consum = c;
    }
 
    public void setPrice(int p) {
        this.price = p;
    }
	
	public boolean equals(Vehicule e) 
	{
		boolean ans;
		if(this.fuelT  == e.fuelT &&
		   this.consum == e.consum && 
		   this.price  == e.price ){ 
			ans = true;
		}
		else
			ans = false;
		return ans;
	}
	
	
}


Car class:
Java
package Package2;

import Package1.Vehicule;

public class Car extends Vehicule {
	String carBrand;
	String carMake;
	int year;
	
	public Car()
	{
		super();
		carBrand = "";
		carMake = "";
		year = 0;
	}
	
	public Car(String b, String m, int y, String f, double c, int p)
	{
		super (f,c,p);
		this.carBrand = b;
		this.carMake = m;
		this.year = y;
	}
	
	public String toString()
	{
		return "This Car is a " + fuelT 
		+ ", consumes " + consum + "liters/100km and costs $" 
		+ price + ". It is a " + year + " " + carBrand 
		+ " " + carMake + ".";
	}
	
	// accessors
	public String getCarBrand() {
        return this.carBrand;
    }
	
	public String getCarMake() {
        return this.carMake;
    }
	
	public int getYear() {
        return this.year;
    }
	
	// mutators
	public void setCarBrand(String b) {
        this.carBrand = b;
    }
	
	public void setCarMake(String m) {
        this.carMake = m;
    }
	
	public void setYear(int y) {
        this.year = y;
    }
	
	public boolean equals(Car e) 
	{
		boolean ans;
		if(this.fuelT  == e.fuelT &&
		   this.consum == e.consum && 
		   this.price  == e.price && 
		   this.carBrand == e.carBrand &&
		   this.carMake == e.carMake &&
		   this.year == e.year){ 
			ans = true;
		}
		else
			ans = false;
		return ans;
	}

}

AnswerRe: for loop to find the lowest price in the array Pin
Richard MacCutchan7-Oct-11 21:27
mveRichard MacCutchan7-Oct-11 21:27 
AnswerRe: for loop to find the lowest price in the array Pin
David Skelly9-Oct-11 22:17
David Skelly9-Oct-11 22:17 
QuestionGreen Field Java Pin
cjb1107-Oct-11 1:34
cjb1107-Oct-11 1:34 
AnswerRe: Green Field Java Pin
TorstenH.7-Oct-11 2:42
TorstenH.7-Oct-11 2:42 
GeneralRe: Green Field Java Pin
Nagy Vilmos7-Oct-11 2:51
professionalNagy Vilmos7-Oct-11 2:51 
AnswerRe: Green Field Java Pin
Nagy Vilmos7-Oct-11 2:57
professionalNagy Vilmos7-Oct-11 2:57 
AnswerRe: Green Field Java Pin
David Skelly7-Oct-11 3:13
David Skelly7-Oct-11 3:13 
GeneralRe: Green Field Java Pin
cjb1107-Oct-11 3:31
cjb1107-Oct-11 3:31 
GeneralRe: Green Field Java Pin
jschell7-Oct-11 8:49
jschell7-Oct-11 8:49 
GeneralRe: Green Field Java Pin
David Skelly9-Oct-11 22:13
David Skelly9-Oct-11 22:13 
AnswerRe: Green Field Java Pin
jschell7-Oct-11 8:59
jschell7-Oct-11 8:59 
Questionhelp: equals() method to compare the attributes Pin
mastdesi6-Oct-11 17:24
mastdesi6-Oct-11 17:24 
AnswerRe: help: equals() method to compare the attributes Pin
TorstenH.6-Oct-11 19:30
TorstenH.6-Oct-11 19:30 
GeneralRe: help: equals() method to compare the attributes Pin
David Skelly6-Oct-11 22:17
David Skelly6-Oct-11 22:17 
GeneralRe: help: equals() method to compare the attributes Pin
TorstenH.7-Oct-11 0:18
TorstenH.7-Oct-11 0:18 
GeneralRe: help: equals() method to compare the attributes Pin
mastdesi7-Oct-11 4:09
mastdesi7-Oct-11 4:09 
GeneralRe: help: equals() method to compare the attributes Pin
Nagy Vilmos7-Oct-11 4:36
professionalNagy Vilmos7-Oct-11 4:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.