Laman Utama Tanya Wiki English Artikel Komuniti E-Mel Ringtones Search

Bantuan | Carian | Ahli | Kalendar

Google

Selamat Datang ( Log In | Daftar )

 
Reply to this topicStart new topic
Inheritance Problem- Leh Tlg Sy X?, Method Overriding
puterililin86
post May 6 2008, 01:42 AM
Kiriman #1


Ahli Baru


Grup: Learning
Kiriman: 4
Penyertaan: 6-May 08
Ahli ke- 47,073



Salam

hi..sy sebenarnya de mslh problem sket ni...
pasal inheritance @ perwarisan
sy nak wat 1 coding utk isytiharkan class Marks utk data members Mark1,Mark2,Mark3 n create subclass nama yg bernama Total Of Marks class yg bleh kira total ke3 3 mark td tu...

kire cmni la soklan dier

Write a program to define a calss Marks with data members Mark1,Marks2, and Mark3. Create a subclass Total of Marks class that has method to find the total of the 3 marks.

utk pgetahuan korunk sumer sy pkai j2sdk1.4.2_05
buat set path pastu run javac

ok ni coding sy

class Total
{
int a;
int mrk()
{
return a+a+a;
}
}

class Marks
{
public static void main (String args[])
{
int result;
Total obj1=new Total();

obj1.a=99;
result=obj1.mrk();
System.out.println("Sila Masukkan Mark1 "+result);
obj1.a=88;
result=obj1.mrk();
System.out.println("Sila Masukkan Mark2 "+result);
obj1.a=77;
result=obj1.mrk();
System.out.println("Sila Masukkan Mark3 "+result);
}
}


ni output yg kua biler run





so trus trg sy ni mmg lem sket bab2 java ni
hopefully kpd sesiapa yg tau tu sudikanlah membantu sy yg dlm kerungsingan lg kebingungan ni
Go to the top of the page
 
+Quote Post
puterililin86
post May 6 2008, 01:54 AM
Kiriman #2


Ahli Baru


Grup: Learning
Kiriman: 4
Penyertaan: 6-May 08
Ahli ke- 47,073



utk pgetahuan korunk sumer

ketiga2 Sila Masukkan Mark1,2 n 3 tu dier kua serentak
n xnak kua one by one
n total tu jd ntah pa pe ntah...

n ni contoh yg coding inheritance yg sy jdikan guide

class Sphere
{
double r;
double sph()
{
return 4.0/3.0*3.14*r*r*r;
}
}
class Cylinder extends Sphere
{
double h;
double cylin()
{
return 3.14*r*r*h;
}
}
class Shape
{
public static void main (String args[])
{
double result;
Sphere obj1=new Sphere();
Cylinder obj2=new Cylinder();
obj1.r=5.0;
result=obj1.sph();
System.out.println("The volume of Sphere from obj1 is "+result+" Cu.Units");
obj2.r=3.0;
result=obj2.sph();
System.out.println("The volume of Sphere from obj2 is "+result+" Cu.Units");
obj2.h=6.0;
result=obj2.cylin();
System.out.println("The volume of Cylinder from obj2 is "+result+" Cu.Units");
}
}


ni output dier



hope de yg sudi membantu...sekian time kasih
Go to the top of the page
 
+Quote Post
Firestarter
post May 6 2008, 07:26 AM
Kiriman #3


Ahli Rajin
****

Grup: Members
Kiriman: 728
Penyertaan: 2-December 06
Dari: Paka ke Dungun
Ahli ke- 21,529



QUOTE(puterililin86 @ May 6 2008, 02:02 AM)
utk pgetahuan korunk sumer

ketiga2 Sila Masukkan Mark1,2 n 3 tu dier kua serentak
n xnak kua one by one
n total tu jd ntah pa pe ntah...


Aku tak paham dengan maksud ko kat atas tu. Macam ada missing perkataan je.


--------------------
Java Specialist Programmer
programming+bisnes logic=headche
Go to the top of the page
 
+Quote Post
MatchMaker
post May 6 2008, 09:48 AM
Kiriman #4


Ahli
***

Grup: Members
Kiriman: 461
Penyertaan: 22-May 04
Dari: Shah Alam
Ahli ke- 679



konsep tu dah ada. untuk taknak ketiga tiga keluar serentak tu rasanya nak kena godek lagi tentang console input.
ni aku dah betulkan sikit daripada apa yg ko buat tu.
lagi satu aku pun tak ingat la daripada soalan tu, sapa yg extends sapa. ehehhe.

CODE
class Total {

    int mark1;
    int mark2;
    int mark3;

    int mrk() {
        return mark1 + mark2 + mark3;
    }
}

class Marks extends Total {

    public static void main(String args[]) {
        int result;
        Total obj1 = new Total();

        obj1.mark1 = 99;
        System.out.println("Sila Masukkan Mark1 " + obj1.mark1);
        obj1.mark2 = 88;
        System.out.println("Sila Masukkan Mark2 " + obj1.mark2);
        obj1.mark3 = 77;
        System.out.println("Sila Masukkan Mark3 " + obj1.mark3);
        result = obj1.mrk();
        System.out.println("Total : " + result);
    }
}


pasal console input
Steps for console based user input:

1. Use the System.in object to create an InputStreamReader object.
2. Use the InputStreamReader object to create a BufferedReader object.
3. Display a prompt to the user for the desired data.
4. Use the BufferedReader object to read a line of text from the user.
5. Do something interesting with the input received from the user.

Would you like to see some code? I thought so. Here it is:

// 1. Create an InputStreamReader using the standard input stream.
InputStreamReader isr = new InputStreamReader( System.in );

// 2. Create a BufferedReader using the InputStreamReader created.
BufferedReader stdin = new BufferedReader( isr );

// 3. Don't forget to prompt the user
System.out.print( "Type some data for the program: " );

// 4. Use the BufferedReader to read a line of text from the user.
String input = stdin.readLine();

// 5. Now, you can do anything with the input string that you need to.
// Like, output it to the user.
System.out.println( "input = " + input );

@dicilok dari http://pages.cs.wisc.edu/~cs302/io/JavaIO.html


--------------------
Sony PSP Slim and Lite PSP-2006 PB
3.71 M33-2 > 3.71 M33-4 > 3.80 M33 > 3.80 M33-2 > 3.80 M33-3 > 3.80 M33-4 > 3.80 M33-5 > 3.90 M33-3
+ 3.80 M33 Popsloader
Canon Ixus 75
Compaq Presario V3751AU
Go to the top of the page
 
+Quote Post
puterililin86
post May 6 2008, 04:50 PM
Kiriman #5


Ahli Baru


Grup: Learning
Kiriman: 4
Penyertaan: 6-May 08
Ahli ke- 47,073



QUOTE(Firestarter @ May 6 2008, 07:34 AM) *
Aku tak paham dengan maksud ko kat atas tu. Macam ada missing perkataan je.



xde yg missing ape pun...mmg e2 jer yg saya type
Go to the top of the page
 
+Quote Post
puterililin86
post May 6 2008, 05:49 PM
Kiriman #6


Ahli Baru


Grup: Learning
Kiriman: 4
Penyertaan: 6-May 08
Ahli ke- 47,073



QUOTE(MatchMaker @ May 6 2008, 09:56 AM) *
konsep tu dah ada. untuk taknak ketiga tiga keluar serentak tu rasanya nak kena godek lagi tentang console input.
ni aku dah betulkan sikit daripada apa yg ko buat tu.
lagi satu aku pun tak ingat la daripada soalan tu, sapa yg extends sapa. ehehhe.

CODE
class Total {

    int mark1;
    int mark2;
    int mark3;

    int mrk() {
        return mark1 + mark2 + mark3;
    }
}

class Marks extends Total {

    public static void main(String args[]) {
        int result;
        Total obj1 = new Total();

        obj1.mark1 = 99;
        System.out.println("Sila Masukkan Mark1 " + obj1.mark1);
        obj1.mark2 = 88;
        System.out.println("Sila Masukkan Mark2 " + obj1.mark2);
        obj1.mark3 = 77;
        System.out.println("Sila Masukkan Mark3 " + obj1.mark3);
        result = obj1.mrk();
        System.out.println("Total : " + result);
    }
}


pasal console input
Steps for console based user input:

1. Use the System.in object to create an InputStreamReader object.
2. Use the InputStreamReader object to create a BufferedReader object.
3. Display a prompt to the user for the desired data.
4. Use the BufferedReader object to read a line of text from the user.
5. Do something interesting with the input received from the user.

Would you like to see some code? I thought so. Here it is:

// 1. Create an InputStreamReader using the standard input stream.
InputStreamReader isr = new InputStreamReader( System.in );

// 2. Create a BufferedReader using the InputStreamReader created.
BufferedReader stdin = new BufferedReader( isr );

// 3. Don't forget to prompt the user
System.out.print( "Type some data for the program: " );

// 4. Use the BufferedReader to read a line of text from the user.
String input = stdin.readLine();

// 5. Now, you can do anything with the input string that you need to.
// Like, output it to the user.
System.out.println( "input = " + input );

@dicilok dari http://pages.cs.wisc.edu/~cs302/io/JavaIO.html




wah MatchMaker, cepatnyer awk dpt settlekan mslh problm sy nie...
awk ni mmg btol matchmaker r
pandai r awk ni...10q 10q byk2 sbb sudi tlg sy...
yg pasal kua output satu2 tu kira tolak tepi dulu nnti sy tgk kemudian..
janji kua output yg sy nak dh kira alhamdulillah...
jenuh sy ubah coding nk bg dier kuakan output yg betol tp xjd2 gak..
rupanya sy isytihar pun dh silap, pastu arahan pn salah lak tu..baru sy nmpak...
tp sy nk tny sket ape fungsi result n return sbnarnya?
n klu kita letak result bg menggantikan obj1.mark1 kt S.O.P tu nape dier cam xleh baca format return tu?


psal spe exstends spe tu citer dier lbh kurang cm ni lar...




btw thx alot erk
Go to the top of the page
 
+Quote Post
MatchMaker
post May 6 2008, 09:19 PM
Kiriman #7


Ahli
***

Grup: Members
Kiriman: 461
Penyertaan: 22-May 04
Dari: Shah Alam
Ahli ke- 679



kalau taknak guna result tu pun boleh
terus je guna return dari mrk()

System.out.println("Total : " + obj1.mrk());

lagi satu mmg kat sini pun dah salah

CODE
int mrk()
{
    return a+a+a;
}


ada 3 nilai kenapa nak add a + a + a plak kan
sepatutnya a + b + c ye tak?

untuk further info, return ni akan pass balik nilai dari mark1 + mark2 + mark3
dan return ni bergantung kepada type waktu kita declare function tu.

contoh:
CODE
int contohInt(){
   return 2 + 2;
}

boolean contohBoolean(){
   return true;
}

String contohString(){
   return "contoh";
}


and byk lagi...


--------------------
Sony PSP Slim and Lite PSP-2006 PB
3.71 M33-2 > 3.71 M33-4 > 3.80 M33 > 3.80 M33-2 > 3.80 M33-3 > 3.80 M33-4 > 3.80 M33-5 > 3.90 M33-3
+ 3.80 M33 Popsloader
Canon Ixus 75
Compaq Presario V3751AU
Go to the top of the page
 
+Quote Post
Firestarter
post May 6 2008, 09:46 PM
Kiriman #8


Ahli Rajin
****

Grup: Members
Kiriman: 728
Penyertaan: 2-December 06
Dari: Paka ke Dungun
Ahli ke- 21,529



Dia tengah belajar lagi tu. Nanti dah pandai cam ko buleh la dia tolong orang lain pulak..

p/s: MactchMaker, bila ko nak ambik scjp?


--------------------
Java Specialist Programmer
programming+bisnes logic=headche
Go to the top of the page
 
+Quote Post
MatchMaker
post May 7 2008, 08:56 AM
Kiriman #9


Ahli
***

Grup: Members
Kiriman: 461
Penyertaan: 22-May 04
Dari: Shah Alam
Ahli ke- 679



off topic:
emmm tak ambik dah kot
aku dah keje, takde masa nak belajar + ambik scjp tu biggrin.gif

so bila ko nyer scjp test tu?


--------------------
Sony PSP Slim and Lite PSP-2006 PB
3.71 M33-2 > 3.71 M33-4 > 3.80 M33 > 3.80 M33-2 > 3.80 M33-3 > 3.80 M33-4 > 3.80 M33-5 > 3.90 M33-3
+ 3.80 M33 Popsloader
Canon Ixus 75
Compaq Presario V3751AU
Go to the top of the page
 
+Quote Post
Firestarter
post May 7 2008, 09:41 AM
Kiriman #10


Ahli Rajin
****

Grup: Members
Kiriman: 728
Penyertaan: 2-December 06
Dari: Paka ke Dungun
Ahli ke- 21,529



Deleted

Kiriman ini telah diubahsuai oleh Firestarter: May 9 2008, 12:22 AM


--------------------
Java Specialist Programmer
programming+bisnes logic=headche
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

 

RSS Lo-Fi Version Time is now: 7th October 2008 - 07:25 PM

© 1998-2008 Khazanah Maya Computer Systems