battle programmers alliance

battle programminga forum for elite programmerswith extreme will powerto sharpen theire skills
 
HomeCalendarFAQSearchMemberlistUsergroupsRegisterLog in
Search
 
 

Display results as :
 
Rechercher Advanced Search
Latest topics
» android mobile app dev APK
Yesterday at 10:01 pm by Moti Barski

» battle programmers alliance new features :
Yesterday at 5:25 pm by Admin

» urban prepper grimoire
Sat Aug 18, 2018 1:03 pm by Moti Barski

» broadcast receiver dead
Sat Aug 04, 2018 7:51 am by Moti Barski

» programming tool : find dif between 2 texts
Tue Jul 31, 2018 6:32 am by Moti Barski

» java class that filter numbers out of a string
Mon Jul 30, 2018 11:20 am by Moti Barski

» android studio execises
Mon Jul 09, 2018 1:43 pm by Moti Barski

» problem with firebase authentication
Thu Jun 28, 2018 9:52 am by Moti Barski

» text art canvas
Wed Jun 20, 2018 8:55 pm by Moti Barski

August 2018
SunMonTueWedThuFriSat
   1234
567891011
12131415161718
19202122232425
262728293031 
CalendarCalendar
Social bookmarking
Social bookmarking digg  Social bookmarking delicious  Social bookmarking reddit  Social bookmarking stumbleupon  Social bookmarking slashdot  Social bookmarking yahoo  Social bookmarking google  Social bookmarking blogmarks  Social bookmarking live      

Bookmark and share the address of battle programmers alliance on your social bookmarking website
Share | 
 

 polymorphism in JAVA eclipse

Go down 
AuthorMessage
Admin
Admin
avatar

Posts : 113
Join date : 2011-08-01

PostSubject: polymorphism in JAVA eclipse   Wed Jan 31, 2018 9:20 pm

polymorphism, exampled via the shape class, and its overridden surface area methode,
in the Main class.

shape super class :
Code:
package com.shapes.objects;

public class Shape {
   
   public Shape() {
      // TODO Auto-generated constructor stub
   }
   public double surfaceArea() {
      return 0;
   }
}

square sub class of shape

Code:
package com.shapes.objects;

public class Square extends Shape {
   private double height;

   public double getHeight() {
      return height;
   }

   public void setHeight(double height) {
      this.height = height;
   }
   public Square(double height) {
      this.height = height;
   }
   public double surfaceArea() {
      return height*height;
   }
}

rectangle sub class of square
Code:
package com.shapes.objects;

public class Rectangle extends Square{
   private double width;

   public double getWidth() {
      return width;
   }

   public void setWidth(double width) {
      this.width = width;
   }

   public Rectangle(double height, double width) {
      super(height);
      this.width = width;
   }
   @Override
   public double surfaceArea() {
      return super.getHeight()*width;
   }
   

}

triangle sub class of shape

Code:
package com.shapes.objects;

public class Triangle extends Shape {
   private double height;
   private double width;
   
   public Triangle(double height, double width) {
      super();
      this.height = height;
      this.width = width;
   }
   public double getHeight() {
      return height;
   }
   public void setHeight(double height) {
      this.height = height;
   }
   public double getWidth() {
      return width;
   }
   public void setWidth(double width) {
      this.width = width;
   }
   @Override
   public double surfaceArea() {
      return (height*width)/2;
   }
   
}

circle class extends (sub class of) shape super class:
Code:
package com.shapes.objects;

public class Circle extends Shape {
   private double Radius;
   public double getRadius() {
      return Radius;
   }
   public void setRadius(double radius) {
      Radius = radius;
   }
   
   public Circle(double radius) {
      super();
      Radius = radius;
   }
   public double surfaceArea() {
      return Radius*Radius*3.1415;
   }
}

main class plays around with the classes using polymorphism to refer to all objects as a shape
and using the override methods of the sub classes even though they were declared as the super class :

Code:
package com.shapes.objects;

import java.util.Scanner;

import org.w3c.dom.css.Rect;

public class Main {

   public static void main(String[] args) {
      // TODO Auto-generated method stub
      Shape sh1 = new Shape();
      System.out.println(sh1.surfaceArea());
      Rectangle rec1 = new Rectangle(10, 5);
      System.out.println(rec1.surfaceArea());
      Shape sh2 = rec1;
      System.out.println(sh2.surfaceArea());
      
      Shape[] shapeArray = new Shape[5];
      
      for (int i = 0; i < shapeArray.length; i++) {
         System.out.println("enter shape");
         Scanner scanner2 = new Scanner(System.in);
         String x2 = scanner2.next();
         shapeArray[i] = getShape(x2);
         System.out.println(shapeArray[i].surfaceArea());
      }
   }
   public static Shape getShape(String form) {
      Shape result = null;
      Scanner scanner11 = new Scanner(System.in);
      switch (form) {
      case "square":
         System.out.println("enter square side size");
         double x1 = scanner11.nextDouble();
         return new Square(x1);
      case "circle":
         System.out.println("enter circle radius");
         double Radius1 = scanner11.nextDouble();
         return new Circle(Radius1);
      case "rectangle":
         System.out.println("enter rectangle height");
         double height = scanner11.nextDouble();
         System.out.println("enter rectangle width");
         double width = scanner11.nextDouble();
         return new Rectangle(height, width);
      case "triangle":
         System.out.println("enter triangle height");
         double Theight = scanner11.nextDouble();
         System.out.println("enter triangle width");
         double Twidth = scanner11.nextDouble();
         return new Triangle(Theight, Twidth);
      default:
         break;
      }
      return result;
   }

}

_________________
kurayami no kagi  _____    
Back to top Go down
View user profile http://yotamarker.forumotion.com
 
polymorphism in JAVA eclipse
Back to top 
Page 1 of 1
 Similar topics
-
» [Help] How to use Selenium RC with Java Eclipse IDE
» Verify command when used with Java,JUNIT and Eclipse
» Getting the error msg 'Error: Could not find or load main class java-jar' while executing the selenium code in eclipse
» Getting error when i run selenium script in Eclipse, please help
» Eclipse setup with selenium RC

Permissions in this forum:You cannot reply to topics in this forum
battle programmers alliance :: battle programming alliance :: chit chat-
Jump to: