views:

94

answers:

4

Hello everybody! I am trying to study some java and cannot get my head around this question! Any help would be really appreciated and would help me in further studies! Thank you so much in advance! Eoin from Dublin, Ireland.

ArrayList of Objects Write a program which works out which is the lowest priced computer in an ArrayList of computer objects.

You should have two classes, one called ComputerTest, and the other called Computer.

In the ComputerTest class you should:

Have a main method which declares an ArrayList of type Computer called computerList, each element of which represents a computer. Initialise the first three entries of the ArrayList Call a static method which prints out the lowest price computer, called lowestPrice The parameter of this method is (i) an ArrayList of type Computer i.e. ArrayList data

Sample output:

The lowest priced computer is the Cheapo400 at 399 euro.

The Computer class should have:

Private instance variables: manufacturer, model, price and quality. Quality is a rating from 1 to 10. public get and set methods to retrieve and set/change the value of the four instance variables a default constructor a constructor that takes four parameters

A: 

You need to create an arrayList of computer objects and then you would use a function that you create such as getComputerPrice(); on each computer in the list in order to determine the cheapest one. When you have it you then display it to the screen in the form of:

document.write('The lowest price computer is the' + computer.getComputerName() +' at '+ computer.getComputerPrice() +' euro');

Jonathan Czitkovics
That is some great help, thank you Jonathan!
Eoin
A: 

I can provide you with a setup for your Computer class:

public class Computer{

private String manufacturer;
private String model;
private short price;
private int rating;

public Computer(){
}
public Computer(String manufacturer, String model, short price, int rating){
this.manufacturer = manufacturer
this.model = model;
this.price = price;
this.rating = rating;
}
//Getters - Setters

}

Hope this helps you getting started.

lunactic
Please don't do people's homework for them. :-) There's a bunch of things written about how to respond to "help me with homework" requests here: http://meta.stackoverflow.com/questions/10811/homework-on-stackoverflow
Dean J
Thanks for the hint :-) I'll consider it in future answers.
lunactic
Its not actually homework, its an excersise I am doing by myself. The reason I posted this is because I have never done ArrayLists before and find it very confusing. I did not ask for it to be completed just for some help, which I got and thanks a million :)
Eoin
here is what I have so far! http://slexy.org/view/s20OzILSPv
Eoin
looks good so far, what you need to do now is what Justin wrote in Pseudo Code.Create an Array-List with Computers in it and pass them to your lowestPrice Method.
lunactic
+1  A: 

Its a completely your home work, Let me restate your problem so You can do it by yourself and learn.

Create a Class "Computer" Which stores the details of the computer Specification like private String manufacturer; private String model; private double price; private int quality; Generate the setter and getters method for the class. IN the above Class have a Constrcutor which sets all the above variables to the default values.

Create a Class "ComputerTest", in this class you will be writing the actual logic where you can find the best Lowest priced Computer.

\i.e if you have 10 computers from Different manufacturers with different price, yuou need to write a comparision method where it picks out the best price and best quality. That will be your output as you say "Cheapo400 at 399 euro"

Thats it, You will be done with your program!!!!

Sample Program

public class Computer {

    private String manufacturer;

    private String model;

    private double price;

    private int rating;

    // Set some default values for Computer class
    public Computer() {

        manufacturer = "Dell";
        model = "Inspiron";
        price = 790;
        rating = 8;
    }

    // Create an object of Computer class which will take in new data
    public Computer(String newManufacturer, String newModel, double newPrice,
            int newRating) {

        manufacturer = newManufacturer;
        model = newModel;
        price = newPrice;
        rating = newRating;
    }

    // Here is the get and set method.

    // get
    public String getManufacturer() {
        return manufacturer;
    }

    public String getModel() {
        return model;
    }

    public double getPrice() {
        return price;
    }

    public int getRating() {
        return rating;
    }

    // set
    public void setManufacturer(String newManufacturer) {
        manufacturer = newManufacturer;
    }

    public void setModel(String newModel) {
        model = newModel;
    }

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

    public void setRating(int newRating) {
        rating = newRating;
    }
}

import java.util.ArrayList;
import java.util.Iterator;

public class ComputerTest {

    ArrayList computerList = new ArrayList();

    public void initialise(){
        computerList = AddComputers(computerList);
    }

    public ArrayList AddComputers(ArrayList<Computer> compList){

        Computer comp = new Computer();
        comp.setManufacturer("Dell");
        comp.setModel("Inspiron");
        comp.setPrice(786.00);
        comp.setRating(8);
        compList.add(comp);

        Computer comp1 = new Computer("HP", "Presario",356.00 , 7);
        compList.add(comp1);

        Computer comp2 = new Computer("IBM", "Thinkpad",566.00 , 4);
        compList.add(comp2);

        Computer comp3 = new Computer("Lenovo", "C Series", 987.00 , 9);
        compList.add(comp3);

        return compList;

    }

    public void lowestPrice(){
        Computer temp = new Computer();
        double lowestPrice = 0.0;
        Iterator iter = computerList.iterator();
        while(iter.hasNext()){

            Computer comp = (Computer) iter.next();
            double price = comp.getPrice();

            if((lowestPrice == 0.0) || (price <= lowestPrice)){
                lowestPrice = price;
                temp.setManufacturer(comp.getManufacturer());
                temp.setPrice(comp.getPrice());
                temp.setRating(comp.getRating());
                temp.setModel(comp.getModel());

            }

        }

        System.out.println("Best Deal Manufacturer " 
                + temp.getManufacturer() + " at $" + temp.getPrice());

    }

    public static void main(String args[]){

        ComputerTest test = new ComputerTest();
        test.initialise();
        test.lowestPrice();

    }

}
harigm
Thank you, great help! I will post what I have done when finished! (Hopefully soon!)
Eoin
Thank you again, here is what I have so far, it did take me a while but I am a beginner. It wont fit in here so I pasted it on slexy: http://slexy.org/view/s20OzILSPv
Eoin
will try to compile a program for you give u
harigm
Thank you Harigm, I am quite stuck now!
Eoin
U got any solution, did u complete
harigm
A: 

I'm assuming you're having trouble writing the lowestPrice method, so here's a little psuedo-code to get you started in the right direction:

public static Computer lowestPrice(ArrayList<Computer> computers)
{
    Computer cheapest = null;

    for each computer in computers
    {
        if cheapest is null, then cheapest = c
        if c is less expensive than cheapest then cheapest = c
    }

    return cheapest;
}
Justin Niessner
These all help greatly!! Thank you very much!
Eoin
I have gotten to this point but am a little confused by the code here..., this is what I have so far http://slexy.org/view/s20OzILSPv
Eoin