Results 1 to 7 of 7
  1. #1
    hien_NU is offline Member
    Join Date
    Jan 2008
    Posts
    5
    Rep Power
    0

    Default questions about using array to store profile

    I'm doing a program including:
    Tasks:
    - Enter user’s profile
    - Output: welcome, user’s season, luck guess
    Specifications:
    - Build a class Profile including the above attributes and methods display welcome, user’s season, and luck guess
    - Create a class Test using Profile allowing user to enter all of user’s profile, for example:
    o Output: What’s your name?
    o Input: Name
    o Output: What are you?
    o Input: Student

    (*) Then the program displays a message:
    “Please select one of the following options, enter 1 or 2 or 3:
    1. Welcome
    2. Your season
    3. Are you lucky today?”

    Using array to store profiles
    Every time repeating the above process
    - If input name exists, the program will display the following output:
    “Your profile is available. Do you update or not?”, If the answer is “Yes”, the next questions are performed, else the program skips to the (*) step.
    - Else: the above process is performed normally.

    I'm not clear about how to use array to store profile which we input.
    Please help me:) :)

  2. #2
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    7

    Default Classes and arrays

    Hello hien_NU.

    Firstly, lets define Profile:
    Java Code:
    public class Profile{
        private String name;
        private String occupation;
        public Profile(String name, String occupation){
            this.name = name;
            this.occupation = occupation;
        }
        // Rest of class: TODO
    }
    To create an array of three objects of instance Profile, you must use the new keyword:
    Java Code:
    Profile[] profiles = new Profile[3];
    The array, profiles, now contains Profile objects that are null referenced. That means that they contain nothing. Now you must create an instances of Profile to populate the profiles array.
    Java Code:
    profiles[0] = new Profile("Ben", "Programmer");
    profiles[1] = new Profile("Tim", "Student");
    profiles[2] = new Profile("Joy", "Actor");
    This demonstrates how to use objects and arrays.

    This code was checked by hand. Hope this helped. :D
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

  3. #3
    hien_NU is offline Member
    Join Date
    Jan 2008
    Posts
    5
    Rep Power
    0

    Default

    Can you give me a full code of 2 class: Profile and Test, please?

  4. #4
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    7

    Default Sorry

    Quote Originally Posted by hien_NU View Post
    Can you give me a full code of 2 class: Profile and Test, please?
    I will help you with your questions, but the final coding is up to you. :p
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

  5. #5
    hien_NU is offline Member
    Join Date
    Jan 2008
    Posts
    5
    Rep Power
    0

    Default

    This is the code of class Profile and class class Test which I did but it seems wrong with class Test......Why is it wrong?:mad:

    import static java.lang.Math.*;
    public class Profile {
    private String name;
    private String job;
    private int day;
    private int month;
    public Profile(String name, String job, int day, int month) {
    this.name = name;

    return day;
    }
    public void setMonth(int Month) {
    month = Month;
    }
    public int getMonth() {
    return month;
    }
    public void displayWelcome() {
    System.out
    .printf(
    "\nHello %s\nYou are a %sAnd were born on %s/%s\nYou are our member from now. Nice to see you.\n",
    getName(), getJob(), getDay(), getMonth());

    System.out.println();
    }

    public void displaySeason() {

    if (getMonth() == 3 | getMonth() == 4 | getMonth() == 5)
    System.out.println("\nYour birth season is Spring.");
    else if (getMonth() == 6 | getMonth() == 7
    | getMonth() == 8)
    System.out.println("\nYour birth season is Summer.");
    else if (getMonth() == 9 | getMonth() == 10
    | getMonth() == 11)
    System.out.println("\nYour birth season is Autumn.");
    else
    System.out.println("\nYour birth season is Winter.");

    System.out.println();
    }
    public void displayLuckGuess() {
    int I = (int) round(getDay() * getMonth() * random() % 4);
    switch (I) {
    case 0:
    System.out.println("\nBAD");
    break;
    case 1:
    System.out.println("\nGOOD");
    break;
    case 2:
    System.out.println("\nBETTER");
    break;
    case 3:
    System.out.println("\nBEST");
    break;
    }
    }
    }

    import java.util.Scanner;

    public class Test {
    public static void main(String args[]) {
    Scanner input = new Scanner(System.in);
    final int NUMBER = 2;
    Profile profiles[] = new Profile[NUMBER];

    for (int counter = 0; counter < NUMBER; counter++) {
    System.out.println("What's your name:");
    profiles[counter].setName(input.nextLine());

    System.out.println("What's your job:");
    profiles[counter].setJob(input.nextLine());

    System.out.println("What's your hobby:");
    profiles[counter].setHobby(input.nextLine());

    System.out.println("What's your major:");
    profiles[counter].setMajor(input.nextLine());

    System.out.println("What's your nation:");
    profiles[counter].setNation(input.nextLine());

    System.out.println("What's your birthday:");
    profiles[counter].setDay(input.nextInt());

    System.out.println("What's your birthmonth:");
    profiles[counter].setMonth(input.nextInt());

    System.out
    .println("Please select one of the following options, enter 1 or 2 or 3:\n1.Welcome\n2.Your season\n3.Are you lucky today?");
    int Option = input.nextInt();
    switch (Option) {
    case 1:
    profiles[counter].displayWelcome();
    break;
    case 2:
    profiles[counter].displaySeason();
    break;
    case 3:
    profiles[counter].displayLuckGuess();
    break;
    }// end switch
    }// end for
    }// end main
    }// end class

  6. #6
    jelly's Avatar
    jelly is offline Member
    Join Date
    Jan 2008
    Location
    Somerset, UK
    Posts
    46
    Rep Power
    0

    Default

    You are never creating a Profile object to store in the array. Arrays are simply containers for objects, you still need to create the objects using the new keyword. In your code you could create each new Profile object as you went round the loop, e.g. change your code to read:

    ...
    for (int counter = 0; counter < NUMBER; counter++) {

    // new line of code to create Profile instance
    profiles[counter] = new Profile();

    System.out.println("What's your name:");
    profiles[counter].setName(input.nextLine());
    ...

    Hope that helps
    Last edited by jelly; 01-07-2008 at 06:32 PM.

  7. #7
    hien_NU is offline Member
    Join Date
    Jan 2008
    Posts
    5
    Rep Power
    0

    Default

    Thanks everyone so much. The first loop is OK. But when the second and later loop begins, it skips sentence "What's your name" and jump into sentence"What's your job?"===>What's the problem here?

    And the second question is:
    Every time repeating the above process
    - If input name exists, the program will display the following output:
    “Your profile is available. Do you update or not?”, If the answer is “Yes”, the next questions are performed, else the program skips to the (*) (mentioned above) step.
    - Else: the above process is performed normally.

Similar Threads

  1. Replies: 5
    Last Post: 07-30-2008, 09:30 AM
  2. Replies: 3
    Last Post: 04-07-2008, 06:02 PM
  3. store file
    By kazitula in forum Java Applets
    Replies: 0
    Last Post: 02-17-2008, 09:45 PM
  4. Replies: 1
    Last Post: 01-10-2008, 01:18 AM

Posting Permissions

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