|
sapa2 leh tlg aku x?? aku dpt soalan mcm ni :
Write a program that prompts the user to enter name, age and gender of five number of person. This program will calculate the average age of the person. Besides, the program also include the following operations:
- Finding the oldest and youngest person so that it finds the positions in the array of the oldest and youngest.
- Searching for a name and delete the entry name from the array. An error message will be displayed if the searching name is not in the array.
- Displaying the names of persons in the array after the deletion has been performed
output dia kena mcm ni:
Enter name:
Hadif
Enter age:
5
Enter gender (M/F):
M
Enter name:
Farrah
Enter age:
21
Enter gender (M/F):
F
Enter name:
Mardhiah
Enter age:
1
Enter gender (M/F):
F
Enter name:
Nazif
Enter age:
2
Enter gender (M/F):
M
Enter name:
Imran
Enter age:
45
Enter gender (M/F):
M
*****************************
Average age: 14.8
Youngest: Mardhiah is 1 years old is at position 2 in the list.
Oldest: Imran is 45 years old is at position 4 in the list.
Name to delete:
Farrah
Name in the array:
Hadif
Mardhiah
Nazif
Imran
[ Last edited by kupu2siang at 28-3-2008 10:19 AM ] |
|
|
|
|
|
|
|
aku dah dpt mcm ni:
import java.util.*;
//import java.io.*;
public class ProcessPersonArray
{
public static void main (String[] args)
{
Person[] person; //declare the person array
person = new Person[2]; //and then create it
Scanner scanner =new Scanner (System.in);
//----------- Create person Array ------------//
String name, inpStr;
int age;
char gender;
for (int i = 0; i < person.length; i++)
{
//read in data values
System.out.print("Enter name:" +"\n");
name =scanner.next();
System.out.print("Enter age:"+ "\n");
age = scanner.nextInt();
System.out.print("Enter gender (M/F):" + "\n");
inpStr = scanner.next();
gender = inpStr.charAt(0);
//create a new Person and assign values
person = new Person();
person.setName ( name );
person.setAge ( age );
person.setGender( gender );
} System.out.println("**********************************");
//------ Compute Average Age --------------//
float sum = 0, averageAge;
for (int i = 0; i < person.length; i++)
{
sum += person.getAge();
}
averageAge = sum / (float) person.length;
System.out.println("Average age: " +averageAge);
System.out.println("\n");
//-- Find the youngest and oldest person ------//
//-- Approach No. 3: Using person reference ---//
Person youngest, //points to the youngest person
oldest; //points to the oldest person
youngest = oldest = person[0];
for (int i = 1; i < person.length; i++)
{
if ( person.getAge() < youngest.getAge() )
{
//found a younger person
youngest = person;
}
else if (person.getAge() > oldest.getAge() )
{
//found an older person
oldest = person;
}
}
System.out.println("Oldest : " + oldest.getName() + " is " + oldest.getAge() + " years old.");
System.out.println("Youngest: " + youngest.getName() + " is " + youngest.getAge() + " years old.");
System.out.println("name to search:");
String searchName = scanner.next();
int i = 0;
while (i<person.length &&
!person.getName().equals(searchName)) {
i++;
}
if (i==person.length) {
System.out.print(searchName + " was not in the array");
}else {
System.out.println(" " + searchName + " " + "at position" + i);
}
}
}
[ Last edited by kupu2siang at 28-3-2008 10:14 AM ] |
|
|
|
|
|
|
|
output aku lak mcm ni:
Enter name:
yanie
Enter age:
23
Enter gender (M/F):
f
Enter name:
bucuk
Enter age:
22
Enter gender (M/F):
m
**********************************
Average age: 22.5
Oldest : yanie is 23 years old.
Youngest: bucuk is 22 years old.
name to search:
yanie
yanie at position0 |
|
|
|
|
|
|
|
soalan aku:
1)mcm ne nak gabungkan search ngan oldest n youngest tu supaya dpt output mcm ni:
Youngest: Mardhiah is 1 years old is at position 2 in the list.
Oldest: Imran is 45 years old is at position 4 in the list.
2)mcm ne nak wat delete??
--->Displaying the names of persons in the array after the deletion has been performed
tlg aku..
[ Last edited by kupu2siang at 28-3-2008 10:20 AM ] |
|
|
|
|
|
|
|
tenet aku slow la.... kalau tak dah tolong search.. "delete data in java array" <-- try google, then masuk dalam forum.sun <--tak silap aku la
aku tak belajar lagi pulak pasal delete data dalam array
apa2 nanti aku update |
|
|
|
|
|
|
|
Reply #4 kupu2siang's post
utk delete..ko try fahamkan code kat bawah ni
- import java.util.ArrayList;
- public class MainClass {
- public static void main(String args[]) {
- ArrayList<String> al = new ArrayList<String>();
- System.out.println("Initial size of al: " + al.size());
- al.add("C");
- al.add("A");
- al.add("E");
- al.add("B");
- al.add("D");
- al.add("F");
- al.add(1, "A2");
- System.out.println("Size of al after additions: " + al.size());
- System.out.println("Contents of al: " + al);
- al.remove("F");
- al.remove(2);
- System.out.println("Size of al after deletions: " + al.size());
- System.out.println("Contents of al: " + al);
- }
- }
Copy the Code |
|
|
|
|
|
|
|
thnks org letrik... |
|
|
|
|
|
|
|
tp kan org_letrik..yg awk kasi tu kita delete drpd coding tu kan..yg sy nak delete drpd output.. mksudnye... nnti akan kuar output - name to delete:
then kita masukkn nama yg kita nak delete kat situ..pastu dia akan kuar list nama yg tggal..
|
|
|
|
|
|
|
|
Reply #8 kupu2siang's post
yep...konsep yang sama..
cara nak implemen berbeza... |
|
|
|
|
|
|
|
xtau ke nak wat mcm yg sy tnye tu?? ngah buntu ni...ari ahad ni dah nak kena anta.. |
|
|
|
|
|
|
|
nah psuedo code..
banyak2 la coding...
nak delete
1. outputkan "Sila masukkan nama untuk delete"
2. ambil nama untuk di delete itu
3. buat carian dalam array untuk nama itu
4. kalau ader buang lokasi array itu lepas tue paparkan senarai yg ader dalam array itu
5. kalau takde prompt "Nama tiada dalam senarai" lepas tue paparkan senarai yg ada.. |
|
|
|
|
|
|
|
Off topic
Aku tak bis menuntut lagi dalam java ni..Baru separuh jalan..Dok paham guna annotation.. |
|
|
|
|
|
|
|
java array tuh.. ko leh pakai arraylist ker, collection ker, vector ker.. huhuh |
|
|
|
|
|
|
|
Sorting umur, gender bla bla bla ni biasa org guna Excel je. Words pun boleh. :re: |
|
|
|
|
|
|
| |
|