Results 1 to 5 of 5

Thread: Arrays

  1. #1
    ayiliath is offline Member
    Join Date
    Oct 2010
    Posts
    2
    Rep Power
    0

    Default Arrays

    Hi

    I am trying to solve an assignment question on Arrays... Pls see the code that i have written so far. Is there an alternative to System.exit(0) statement?

    import java.util.*;

    public class Computer
    {
    static String name;
    static int size;
    static int speed;
    static String make;
    static double price;
    static int quantity;

    public Computer(String n,int s,int sp,String m,double p,int q)
    {
    name=n;
    size=s;
    speed=sp;
    make=m;
    price=p;
    quantity=q;
    }

    public String getName()
    {
    return name;
    }

    public int getSize()
    {
    return size;
    }

    public int getSpeed()
    {
    return speed;
    }

    public String getMake()
    {
    return make;
    }

    public double getPrice()
    {
    return price;
    }

    public int getQuantity()
    {
    return quantity;
    }

    public void valid()
    {
    if((price==0)||(price<0))
    {
    System.out.println("Please enter price");
    System.exit(0);
    }
    else if((quantity==0)||(quantity<0))
    {
    System.out.println("Please enter quantity ");
    System.exit(0);
    }
    else
    {
    displayData();
    }
    }

    The code in main() displayed below reads the input from user and calls the constructor. I am unable to write the displayData() function. Can someone help?





    public static void main(String args[])
    {
    System.out.println("Enter details");
    Scanner input=new Scanner(System.in);
    name=input.next();
    size=input.nextInt();
    speed=input.nextInt();
    make=input.next();
    price=input.nextDouble();
    quantity=input.nextInt();
    Computer comp=new Computer(name,size,speed,make,price,quantity);
    comp.valid();
    }

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Do you actually itend for the message to be displayed and the program to end? Because exit() will end the program rather abruptly.

    In general you wouldn't do this as it's rather inflexible. Better to return a boolean value to the caller (the method that uses valid()) and let the caller decide what to do.

    Like the following:

    Java Code:
        /** 
         * Displays the data providing it's valid, otherwise prints a message
         * @return  true  iff the price or quantity is nonpositive
         */
    public boolean valid()
    {
        if((price==0)||(price<0))
        {
            System.out.println("Please enter price");
            return false;
        }
        else if((quantity==0)||(quantity<0))
        {
            System.out.println("Please enter quantity ");
            return false;
        }
        else
        {
            displayData();
            return true;
        }
    }
    
    public static void main(String args[])
    {
        System.out.println("Enter details");
        Scanner input=new Scanner(System.in);
        name=input.next();
        size=input.nextInt();
        speed=input.nextInt();
        make=input.next();
        price=input.nextDouble();
        quantity=input.nextInt();
        Computer comp=new Computer(name,size,speed,make,price,quantity);
        if(!comp.valid()) 
        {
            return; // this is a deliberate decision by the caller to exit the program
        }
        // you could carry on if required knowing that the data is good
    }

    valid() is probably not a very descriptive name.

    What have you tried for Computer's displayData() method?

  3. #3
    ayiliath is offline Member
    Join Date
    Oct 2010
    Posts
    2
    Rep Power
    0

    Default Arrays

    Hi

    Thanks so much for the feedback. I shall follow your advise on system.exit statement.

    Reg displayData(), i would have to write code that can capture an array of objects. I really am lost with the standard array statements

    Computer[] c = new CdRecord[5];
    c[0] = new Computer();


    I have put together part of the code as below:

    public void displayData()
    {
    //include code to add Computer objects into an array, named 'c'

    int i = 0;

    for(i=0;i<array.length;i++)
    {
    System.out.println("Computer Name: "+c.getName()+ "\nRAM Size :"+c.getSize()+"\nProcessor Make: "+c.getMake()+"\nProcessor Speed: "+c.getSpeed()+"\nQuantity: "+c.getQuantity()+"\nTotal Price of Computers: "+c.getPrice());
    }
    }

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Reg displayData(), i would have to write code that can capture an array of objects. I really am lost with the standard array statements

    I'm not completely sure what this means.


    Perhaps you should start by studying and revising how Java handles arrays (and what arrays are etc). Eg Lesson: Language Basics (The Java™ Tutorials > Learning the Java Language).

    Break your problem down into small steps. If you get stuck on one particular step describe what you are trying to do (even quote the question). Post your ocde (including the code tags as mentioned above), and remember to say what the code is doing as well as what you want it to do.

Similar Threads

  1. store array of arrays in array of arrays
    By joost_m in forum New To Java
    Replies: 4
    Last Post: 04-19-2010, 10:32 AM
  2. Arrays.sort... why sorting all arrays in class?
    By innspiron in forum New To Java
    Replies: 6
    Last Post: 03-23-2010, 01:40 AM
  3. A little help with arrays..
    By zeppelin in forum New To Java
    Replies: 8
    Last Post: 01-05-2009, 12:33 PM
  4. Arrays
    By bunbun in forum New To Java
    Replies: 1
    Last Post: 04-09-2008, 02:24 AM
  5. new to arrays
    By jimJohnson in forum New To Java
    Replies: 1
    Last Post: 04-08-2008, 02:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •