CariDotMy

 Forgot password?
 Register

ADVERTISEMENT

View: 2465|Reply: 18

java array

[Copy link]
Post time 28-3-2008 10:09 AM | Show all posts |Read mode
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 ]
Reply

Use magic Report


ADVERTISEMENT


 Author| Post time 28-3-2008 10:10 AM | Show all posts
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 ]
Reply

Use magic Report

 Author| Post time 28-3-2008 10:15 AM | Show all posts
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
Reply

Use magic Report

 Author| Post time 28-3-2008 10:18 AM | Show all posts
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 ]
Reply

Use magic Report

Post time 28-3-2008 10:38 AM | Show all posts
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

Use magic Report

Post time 28-3-2008 11:01 AM | Show all posts

Reply #4 kupu2siang's post

utk delete..ko try fahamkan code kat bawah ni


  1. import java.util.ArrayList;

  2. public class MainClass {
  3.   public static void main(String args[]) {
  4.     ArrayList<String> al = new ArrayList<String>();

  5.     System.out.println("Initial size of al: " + al.size());

  6.     al.add("C");
  7.     al.add("A");
  8.     al.add("E");
  9.     al.add("B");
  10.     al.add("D");
  11.     al.add("F");
  12.     al.add(1, "A2");

  13.     System.out.println("Size of al after additions: " + al.size());

  14.     System.out.println("Contents of al: " + al);

  15.     al.remove("F");
  16.     al.remove(2);

  17.     System.out.println("Size of al after deletions: " + al.size());
  18.     System.out.println("Contents of al: " + al);
  19.   }
  20. }
Copy the Code
Reply

Use magic Report

Follow Us
 Author| Post time 28-3-2008 11:09 AM | Show all posts
thnks org letrik...
Reply

Use magic Report

 Author| Post time 28-3-2008 11:12 AM | Show all posts
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

Use magic Report


ADVERTISEMENT


Post time 28-3-2008 11:24 AM | Show all posts

Reply #8 kupu2siang's post

yep...konsep yang sama..

cara nak implemen berbeza...
Reply

Use magic Report

 Author| Post time 28-3-2008 11:25 AM | Show all posts
xtau ke nak wat mcm yg sy tnye tu?? ngah buntu ni...ari ahad ni dah nak kena anta..
Reply

Use magic Report

Post time 28-3-2008 02:19 PM | Show all posts
upah dulu...

saya nak mata awak...
Reply

Use magic Report

Post time 28-3-2008 02:21 PM | Show all posts
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..
Reply

Use magic Report

 Author| Post time 28-3-2008 04:50 PM | Show all posts

Reply #12 stingbeh's post

leh kasi codingnye x??x reti ar...
Reply

Use magic Report

 Author| Post time 28-3-2008 04:52 PM | Show all posts

Reply #12 stingbeh's post

leh kasi codingnye x??x reti ar...
Reply

Use magic Report

Post time 28-3-2008 07:44 PM | Show all posts
macam paham sikit..

nak try buat la soklan ni..

lagi2 exam nak dekat ni
Reply

Use magic Report

 Author| Post time 28-3-2008 11:19 PM | Show all posts

Reply #15 kmkd's post

ko bljr kat ne???
Reply

Use magic Report


ADVERTISEMENT


Post time 5-4-2008 08:16 PM | Show all posts
Off topic

Aku tak bis menuntut lagi dalam java ni..Baru separuh jalan..Dok paham guna annotation..
Reply

Use magic Report

Post time 5-4-2008 08:51 PM | Show all posts
java array tuh.. ko leh pakai arraylist ker, collection ker, vector ker.. huhuh
Reply

Use magic Report

Post time 25-4-2008 07:42 AM | Show all posts
Sorting umur, gender bla bla bla ni biasa org guna Excel je. Words pun boleh. :re:
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

 

ADVERTISEMENT



 

ADVERTISEMENT


 


ADVERTISEMENT
Follow Us

ADVERTISEMENT


Mobile|Archiver|Mobile*default|About Us|CariDotMy

22-1-2025 10:02 AM GMT+8 , Processed in 0.086609 second(s), 32 queries , Gzip On, Redis On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

Quick Reply To Top Return to the list