It is currently Thu Mar 28, 2024 9:26 am



Reply to topic  [ 54 posts ]  Go to page Previous  1, 2, 3, 4
anyone here do Java? 
Author Message
Emperor
User avatar

Joined: Wed Apr 16, 2003 1:25 am
Posts: 2560
Reply with quote
Post 
http://en.wikipedia.org/wiki/Polymorphi ... rogramming

Already done. See the example with one parent and two childs with the different behaviour. :)

_________________
++


Wed May 03, 2006 7:05 am
Profile WWW
Minor Diety
User avatar

Joined: Fri Apr 11, 2003 2:17 pm
Posts: 7721
Location: Centre of the sun
Reply with quote
Post 
I dont recognise the syntax in those examples.

Okay, how about a sample program in this context:

One class is implemented to work out the dimensions of a rectangle. In this class there is a default constructor that initiates the a rectangle object with 2 values of 0. Another constructor uses parameter to initiate the rectangle object with 2 specific values.

Use polymorphism and inheritance to implement a class to work oout the area and perimiter of a right angled triangle.

_________________
"Well a very, very hevate, ah, heavy duh burtation tonight. We had a very derrist derrison, bite, let's go ahead and terrist teysond those fullabit who have the pit." - Serene Branson


Wed May 03, 2006 8:41 am
Profile
Felix Rex
User avatar

Joined: Fri Mar 28, 2003 6:01 pm
Posts: 16646
Location: On a slope
Reply with quote
Post 
sounds like you're trying to get Gfree to do your homework. :roll:

Not that I'm one to talk...I was recently paid to do someone's web design homework. :p

_________________
They who can give up essential liberty to obtain a little temporary safety, deserve neither liberty nor safety.


Wed May 03, 2006 8:58 am
Profile WWW
Minor Diety
User avatar

Joined: Fri Apr 11, 2003 2:17 pm
Posts: 7721
Location: Centre of the sun
Reply with quote
Post 
With Java, I have the opportunity to learn without spending hours studying like all the other subjects.

_________________
"Well a very, very hevate, ah, heavy duh burtation tonight. We had a very derrist derrison, bite, let's go ahead and terrist teysond those fullabit who have the pit." - Serene Branson


Wed May 03, 2006 9:19 am
Profile
Emperor
User avatar

Joined: Wed Apr 16, 2003 1:25 am
Posts: 2560
Reply with quote
Post 
Well you should already know almost all you need to get this one working. You know to override the function? Fine.

1. Make the parent class for classes rectangle and triangle. It should have two protected doubles. Now make the constructor which takes two double values and initializes these two variables. Fine. Now make two public functions: perimeter and area, which take no parameters but return double value. Make their bodies and let them return allways 0.

2. Now make two new public classes: rectangle and triangle, which inherit the class parent. In both classes you should implement:
- default constructor, which call only super(0,0);
- constructor which takes two values e.g. a and b and call olny super(a,b);.
- the function perimeter.
- the function area.

When you think you are done, make the main class with public static void main function and run the following code

Code:
      parent a = new triangle(3,4);
      parent b = new rectangle(3,4);
      
      System.out.println("Triangle:  p = "+a.perimeter()+"; a = "+a.area());
      System.out.println("Rectangle: p = "+b.perimeter()+"; a = "+b.area());


If it compiles and writes:
Code:
Triangle:  p = 12.0; a = 6.0
Rectangle: p=14.0; a = 12.0

You've done it well.

After you've done it, you can try to use abstract classes and methods.

Ok. For this one I'll give you how the start should look like.

Code:
abstract class parent {
   
   protected double h,l;
   
   public parent(double hh,double ll)
   {
      h = hh;
      l = ll;
   }
   
   public abstract double perimeter();
   public abstract double area();
}


Oh yes, I forgot: to get sqrt from some number, use Math.sqrt.
And if you wonder what is super, klick the link in this line.

Good luck. :wink:

_________________
++


Wed May 03, 2006 12:23 pm
Profile WWW
Minor Diety
User avatar

Joined: Fri Apr 11, 2003 2:17 pm
Posts: 7721
Location: Centre of the sun
Reply with quote
Post 
Ok thanks. Ill give it a look over.

_________________
"Well a very, very hevate, ah, heavy duh burtation tonight. We had a very derrist derrison, bite, let's go ahead and terrist teysond those fullabit who have the pit." - Serene Branson


Thu May 04, 2006 5:05 am
Profile
Minor Diety
User avatar

Joined: Fri Apr 11, 2003 2:17 pm
Posts: 7721
Location: Centre of the sun
Reply with quote
Post 
New problem:

I've got a problem with the following method:

Code:
public void saveArr(String[] arrToCombine)
    {
        int i = 0;
        String strVal = "";
       
        while (arrToCombine[i] != null)
        {
            strVal.concat(arrToCombine[i]+"#");
           
            i++;
            System.out.println("strVal = "+strVal);

}
        }


Instead of printing something like:

strVal = a#
strVal = a#b#
strVal = a#b#c#
strVal = a#b#c#d#

It prints this:

strVal =
strVal =
strVal =
strVal =

Any ideas?

_________________
"Well a very, very hevate, ah, heavy duh burtation tonight. We had a very derrist derrison, bite, let's go ahead and terrist teysond those fullabit who have the pit." - Serene Branson


Thu Jun 08, 2006 5:49 am
Profile
Minor Diety
User avatar

Joined: Fri Apr 11, 2003 2:17 pm
Posts: 7721
Location: Centre of the sun
Reply with quote
Post 
Ah solved it. String is an immutable object.

_________________
"Well a very, very hevate, ah, heavy duh burtation tonight. We had a very derrist derrison, bite, let's go ahead and terrist teysond those fullabit who have the pit." - Serene Branson


Fri Jun 09, 2006 3:50 am
Profile
Emperor
User avatar

Joined: Wed Apr 16, 2003 1:25 am
Posts: 2560
Reply with quote
Post 
From my experience, it is allways recommended to post the solution because of the other people, who'd stumble here and read the topic. Many of them would not expect just puzzles but the solutions as well.

The problem in given code was that the methode concat returns new concatenated string insted of concateting the old one (as expected, I assume). So the line
Code:
strVal.concat(arrToCombine[i]+"#");

did just nothing. The variable strVal passes absolutely unchanged. But a little correction:
Code:
strVal = strVal.concat(arrToCombine[i]+"#");

would do the job.

_________________
++


Fri Jun 09, 2006 8:04 am
Profile WWW
Display posts from previous:  Sort by  
Reply to topic   [ 54 posts ]  Go to page Previous  1, 2, 3, 4

Who is online

Users browsing this forum: No registered users and 8 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by STSoftware.