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
» mini A.I puzzles
Mon Apr 23, 2018 11:38 am by Moti Barski

» java and MySQL
Sun Apr 15, 2018 8:57 pm by Moti Barski

» java convert string to unicode
Sun Mar 25, 2018 8:40 pm by Admin

» java eclipse oxygen grimoire
Sat Mar 24, 2018 8:31 pm by Admin

» problem java dynamic web
Sat Mar 24, 2018 6:10 am by Admin

» auto programming dev
Wed Mar 14, 2018 8:42 pm by Admin

» JAVA design patterns
Wed Mar 07, 2018 9:53 pm by Admin

» vb.net morse ear with net lingo beef up:
Sun Mar 04, 2018 10:30 pm by Moti Barski

» java concept input output class
Tue Feb 20, 2018 7:21 pm by Admin

April 2018
SunMonTueWedThuFriSat
1234567
891011121314
15161718192021
22232425262728
2930     
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 : 105
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 https://yotamarker.justforum.net
 
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: