Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-06-2008, 07:47 PM
Member
 
Join Date: Jan 2008
Posts: 5
hien_NU is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 01-06-2008, 08:10 PM
tim's Avatar
tim tim is offline
Senior Member
 
Join Date: Dec 2007
Location: South Africa
Posts: 334
tim is on a distinguished road
Classes and arrays
Hello hien_NU.

Firstly, lets define Profile:
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:
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.
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.
__________________
If your ship has not come in yet then build a lighthouse.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 01-07-2008, 07:42 AM
Member
 
Join Date: Jan 2008
Posts: 5
hien_NU is on a distinguished road
Can you give me a full code of 2 class: Profile and Test, please?
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 01-07-2008, 10:42 AM
tim's Avatar
tim tim is offline
Senior Member
 
Join Date: Dec 2007
Location: South Africa
Posts: 334
tim is on a distinguished road
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.
__________________
If your ship has not come in yet then build a lighthouse.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 01-07-2008, 12:42 PM
Member
 
Join Date: Jan 2008
Posts: 5
hien_NU is on a distinguished road
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?

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
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 01-07-2008, 02:21 PM
jelly's Avatar
Member
 
Join Date: Jan 2008
Location: Somerset, UK
Posts: 46
jelly is on a distinguished road
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 08:32 PM.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 01-08-2008, 07:03 AM
Member
 
Join Date: Jan 2008
Posts: 5
hien_NU is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How do you read from a file, and then store the info in an array? szimme101 New To Java 5 07-30-2008 11:30 AM
How would you get information from a file and then store it in an array? szimme101 Advanced Java 3 04-07-2008 08:02 PM
store file kazitula Java Applets 0 02-17-2008 11:45 PM
How an array can check that profile whether exists or not??? hien_NU New To Java 1 01-10-2008 03:18 AM


All times are GMT +3. The time now is 02:39 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org