BTC USD 81,179.9 Gold USD 4,378.12
Time now: Jun 1, 12:00 AM

[WTA]problem java

vquad

Banned
Messages
2,644
Joined
Jan 25, 2010
Messages
2,644
Reaction score
39
Points
30
Salam semua..saya nak mintak tolong otai2 sume bg tips psal java program yg saya buat nie.. pening dok pikir algorithm die..xkena jek

Write a Java application for a personal phone directory which contains first names and phone numbers for 30
people. Assign names and phone numbers for the first 3 people (use your own data). Prompt the user for a name,
and if the name is found in the list, display the corresponding phone number. If the name is not found in the list,
prompt the user for a phone number, and add the new name and phone number to the list. Continue to prompt the
user for names until the user enters “quit”. After the arrays are full (containing 30 names), do not allow the user to
add new entries.


Class phone

public class Phone{

private String name;
private int pNo;

public Phone(String name,int pNo){
this.name=name;
this.pNo=pNo;
**

public String getname(){

return name;
**
public int pNo(){
return pNo;
**

public void setName(){
this.name=name;
**
public void setPNo(){
this.pNo=pNo;
**


**



Class testPhone

import static javax.swing.JOptionPane.*;

public class TestPhoneBook{
public static void main(String []args){
String name;
int pNo;
int lastCount=3;
int count=0;
String ans;
Phone[] phone= new Phone[30];

phone[0]=new Phone("rashid",0324);
phone[1]=new Phone("azian",330);
phone[2]=new Phone("aziani",340);




do {

name=showInputDialog("Enter name : ");
//pNo=Integer.parseInt(showInputDialog("Enter Phone number :"));

if(name.equals(phone[count])){

showMessageDialog(null,"The name you entered is on the list.\nThe name and phone number is: "+phone[count]);
**

else{


showMessageDialog(null,"The name you entered is not in the system!!!\nPlease key in name and phone number");
name=showInputDialog("Enter name : ");
pNo=Integer.parseInt(showInputDialog("Enter Phone number :"));
phone[count]=new Phone(name,pNo);

**

ans = showInputDialog("Do you want continue? yes/no");




count++;
**while(count<30 || ans.equalsIgnoreCase("yes"));

System.out.println("Sorry you have entered the maximum amount of contact!!!\nThank you for using this PHONEBOOK!!! goodbye");

**
**
 
Last edited:
memang ada class Phone ke? Memang kena create new object Phone ke? satu lagi u ada belajar maps dalam java tak?
 
Salam semua..saya nak mintak tolong otai2 sume bg tips psal java program yg saya buat nie.. pening dok pikir algorithm die..xkena jek

Write a Java application for a personal phone directory which contains first names and phone numbers for 30
people. Assign names and phone numbers for the first 3 people (use your own data). Prompt the user for a name,
and if the name is found in the list, display the corresponding phone number. If the name is not found in the list,
prompt the user for a phone number, and add the new name and phone number to the list. Continue to prompt the
user for names until the user enters “quit”. After the arrays are full (containing 30 names), do not allow the user to
add new entries.

PHP:
[COLOR="DarkRed"]Class phone[/COLOR]
public class Phone{

    private String name;
    private int pNo;
    
    public Phone(String name,int pNo){
        this.name=name;
        this.pNo=pNo;
    **
    
    public String getname(){
        
        return name;
    **
    public int pNo(){
        return pNo;
    **
    
    public void setName(){
        this.name=name;
    **
    public void setPNo(){
        this.pNo=pNo;
    **
 **

PHP:
[COLOR="Red"]Class testPhone[/COLOR]

import static javax.swing.JOptionPane.*;

    public class TestPhoneBook{
        public static void main(String []args){
            String name;
            int pNo;
            int lastCount=3;
            int count=0;
            String ans;
            Phone[] phone= new Phone[30];
           
            phone[0]=new Phone("rashid",0324);
            phone[1]=new Phone("azian",330);
            phone[2]=new Phone("aziani",340);
                 
            do {
               
                name=showInputDialog("Enter name : ");
                //pNo=Integer.parseInt(showInputDialog("Enter   Phone number :"));
               
                    if(name.equals(phone[count])){
                       
                        showMessageDialog(null,"The name you entered is on the list.\nThe name and phone number is: "+phone[count]);
                    **
                    
                    else{
                         
                            showMessageDialog(null,"The name you entered is not in the system!!!\nPlease key in name and phone number");
name=showInputDialog("Enter name :");                            pNo=Integer.parseInt(showInputDialog("Enter Phone number :"));
                            phone[count]=new Phone(name,pNo);    
                    **
                    ans = showInputDialog("Do you want continue? yes/no");
                    
                
               
        
                count++;
            **while(count<30 || ans.equalsIgnoreCase("yes"));
            
            System.out.println("Sorry you have entered the maximum amount of contact!!!\nThank you for using this PHONEBOOK!!! goodbye");
        
    **
**

first of all.. mohon pakai tag php atau code tag kalau anda ada provide source code sekali, ini bagi memudahkan orang membantu anda, kerana ianya nampak lebih teratur berbanding jika tidak menggunakan tag tersebut dan tulis kod tu dengan elok.. :).

so dari koding ni aku tengok , searching tu akan ada masalah yang mana memang takkan boleh check sama ada nama orang tu dah exist atau belum

untuk aku, solution dia, buat nested loop.. satu loop dari 3 - 29 (external loop)
dan satu lagi loop dari 0 - current position of external loop (internal loop)
dalam internal loop ni buat search.. kalau ada data yang sama, apa mesej akan keluar, kalau tiada data yang sama, maka process menambah data baru akan dilakukan di luar daripada internal loop

dan showMessageDialog function adalah static method bagi kelas JOptionPane, so panggil method tu secara direct tanpa menggunakan nama kelas dihadapan method tersebut akan menjadikan ianya error :).. the right way is JOptionPane.showMessageDialog(""); :)
 
Last edited:
thanks ke[ada yang reply..problem dah solved..
 

dan showMessageDialog function adalah static method bagi kelas JOptionPane, so panggil method tu secara direct tanpa menggunakan nama kelas dihadapan method tersebut akan menjadikan ianya error :).. the right way is JOptionPane.showMessageDialog(""); :)


bro kat atas tu saya buat import static.javax.swing.JOptionPane.*;

so xperlu dah letak JOptionPane.showMessageDialog("");... :D:D
 
Back
Top
Log in Register