Results 1 to 4 of 4
  1. #1
    bannow is offline Member
    Join Date
    Apr 2010
    Posts
    2
    Rep Power
    0

    Default Objects and Arrays

    My name is Aaron Boyse and i am having trouble with a Java program for Object Orientation and Arrays. Need Help!! Heres the code please have a look.

    class project2

    // Student Name : Aaron Boyse
    // Student Id Number : C00133201
    // Date : 26/3/2010
    // Purpose : Object Orientation - Project 2



    {


    public static void menu()//menu method

    {
    //Input

    System.out.println(" Menu ");
    System.out.println(" ---------- ");
    System.out.println();
    System.out.println(" 1. Add ");
    System.out.println(" 2. Delete ");
    System.out.println(" 3. List All ");
    System.out.println(" 4. Exit ");


    }

    public static void clearScreen()//method to clear screen

    {
    //Variables

    int index;

    for (index = 0; index < 12; index ++)
    {
    System.out.println();//outputs nothing
    }
    }



    public static int findPlace(Student theArray[], int theNumber, int noOfValues)//findPlace method

    {
    //Variables

    int step;
    int place;

    //Initialize the Variables
    step = 0 ;
    while (step < noOfValues && theNumber<(theArray[step].getaverage()) )//while loop
    {
    step ++;
    }
    place = step ; // Finds the correct location of place

    return place;
    }


    public static int addOne(Student theArray[], int place, Student theObject, int noOfValues)//addOne method
    {
    //Variables

    int step;

    if(noOfValues == 0) //What the program does if noOfValues = 0
    {
    theArray[0] = theObject;
    noOfValues ++;
    }
    else
    {
    for(step = noOfValues-1; step >=place; step --)
    {
    theArray[step + 1] = theArray[step];
    }
    theArray[place] = theObject;
    noOfValues ++;
    }
    return noOfValues; //returns the number of values in the Array
    }


    public static int deleteOne(Student theArray[], int theNumber, int noOfValues)//method to initialize the variables theArray[], theNumber and noOfValues
    {

    //Variables

    int step;
    int whatOne;

    for(step = theNumber; step < noOfValues -1; step ++)//for loop
    {
    theArray[step] = theArray[step +1];
    }

    theArray[noOfValues -1] = new Student("","",0);
    -- noOfValues ;

    return noOfValues;//return method

    }


    public static int search(Student theArray[], String theNumber, int noOfValues)//method to initialize theArray[], theNumber and noofValues
    {
    //Variables

    int step;
    int place;

    place = -1;
    step = 0;

    while(step < noOfValues && theArray[step].getId().equals(theNumber) == false)//while loop
    {
    step ++ ;
    }
    if(step < noOfValues)
    {
    place = step ;
    }

    return place;
    }

    public static void options()//options method

    {
    //Variables and Initialization of:

    char option;
    Student studentArray[];
    Student tempObject;
    String currentStudentName;
    String currentStudentID;
    int noOfElements;
    int currentStudentAverage;
    int index;
    int ID;
    int location = 0;
    int place;

    studentArray = new Student[25];
    noOfElements = 0;

    menu();
    System.out.println();
    System.out.print("Enter Option Required (1-4) ->> ");//message to say 4 options required please enter one
    option = EasyIn.getChar();
    while (option != '4')//while
    {

    switch(option) //switch option
    {
    //Input
    case '1': System.out.print("Enter ID ->> ");//message to enter ID
    System.out.println();//prints a space
    currentStudentID = EasyIn.getString();
    place = search(studentArray, currentStudentID, noOfElements);
    if(place != -1)
    {
    System.out.print("Cannot Add ID " +currentStudentID+ " ,It already exists");//message to say ID already exists
    }
    else
    {
    System.out.print("Enter Student Name ->> ");//enter student name
    currentStudentName = EasyIn.getString();
    System.out.println();
    System.out.print("Enter Student Average ->> ");//enter student average
    currentStudentAverage = EasyIn.getInt();
    System.out.println();

    tempObject = new Student(currentStudentID, currentStudentName, currentStudentAverage);
    location = findPlace(studentArray, currentStudentAverage, noOfElements);
    noOfElements = addOne(studentArray, location, tempObject, noOfElements);
    }
    break;

    case '2': System.out.print("Enter an ID to Delete ->>");//enter ID to delete
    currentStudentID = EasyIn.getString();
    System.out.println();
    place = search(studentArray, currentStudentID, noOfElements);
    if(place == -1)
    {
    System.out.print("Cannot Delete ID " + currentStudentID + " ,It doesn't exists");//message for when ID doesnt exist
    }
    else
    {
    noOfElements = deleteOne(studentArray, place, noOfElements);
    System.out.println("The ID "+currentStudentID+ " has been deleted.");//message for when ID has been deleted
    }
    break;

    case '3': for(index = 0; index < noOfElements; index++)
    {
    System.out.println(studentArray[index].toString());//Student Array to get string
    }
    break;

    default: System.out.println("Invalid Option!");// default message and message for Invalid Option
    EasyIn.pause();//EasyIn.pause method
    }
    clearScreen();//clears the screen
    menu();//call the menu
    System.out.println();
    System.out.println("Enter Option Required (1-4) ");//System asks for four options
    option = EasyIn.getChar();//EasyIn method
    }
    }

    public static void main(String[] args)//main method

    {
    options();//asks for the different options
    }
    }//main class

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,525
    Blog Entries
    7
    Rep Power
    20

    Default

    Yes, and your question is?

    kind regards,

    Jos

  3. #3
    bannow is offline Member
    Join Date
    Apr 2010
    Posts
    2
    Rep Power
    0

    Smile Objects

    I need help because when i enter in the student name and student id it'll switch the values around.

  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
    20

    Default

    Can you please ask your question more specifically. Reading your unformated code is really difficult. And also please use code tags next time when you posting code segments here in the forum. If you don't know how to do that, check on my forum signature.
    Last edited by Eranga; 04-19-2010 at 07:19 PM. Reason: added more

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. How to map 2 different objects together?
    By einnhann in forum New To Java
    Replies: 5
    Last Post: 12-22-2009, 09:42 AM
  4. Replies: 1
    Last Post: 01-22-2009, 04:25 PM
  5. [SOLVED] Arrays of Objects with Subscripts
    By Sidmyre in forum New To Java
    Replies: 5
    Last Post: 12-12-2008, 02:18 AM

Tags for this Thread

Posting Permissions

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