Hey guys... :) for this time I want to share my project. I call it MyGeometric. I will show you how to build a concept about OOP in java. Okay guys, those are the code.
(Rectangle.java)
package Geo1;
public class Rectangle {
protected double Long,Width;
public void setSize(double l,double w){
Long=l;
Width=w;
}
public double getWideArea(){
return Long*Width;
}
public double getSurroundArea(){
return 2*(Long+Width);
}
public void printObject(){
System.out.println("Long\t: "+Long);
System.out.println("Width\t: "+Width);
System.out.println("Wide\t: "+getWideArea());
System.out.println("Surround\t: "+getSurroundArea());
}
}
(Square.java)
package Geo1;
public class Square extends Rectangle {
protected double Side;
public void setSize(double s){
super.setSize(s, s);
Side=s;
}
@Override
public void printObject(){
System.out.println("Side\t: "+Side);
System.out.println("Wide\t: "+getWideArea());
System.out.println("Surround\t: "+getSurroundArea());
}
}
(MyGeometric.java)
package mygeometric;
import Geo1.*;
import java.util.Scanner;
class Quadrangle extends Square{
private String ObjName;
private int ObjType;
Scanner s=new Scanner(System.in);
public void setObject(String name,int t){
ObjType=t;
switch (ObjType){
case 1:
double l,w;
System.out.print("Long : ");
l=s.nextDouble();
System.out.print("Width : ");
w=s.nextDouble();
setSize(l, w);
ObjName="Rectangle " + name;
break;
case 2:
double sd;
System.out.print("Side : ");
sd=s.nextDouble();
setSize(sd);
ObjName="Square " + name;
break;
}
}
@Override
public void printObject(){
System.out.println("Data of " + ObjName );
if(ObjType==1){
System.out.println("Long\t: "+Long);
System.out.println("Width\t: "+Width);
System.out.println("Wide\t: "+getWideArea());
System.out.println("Surround: "+getSurroundArea());
}else if(ObjType==2){
System.out.println("Side\t: "+Side);
System.out.println("Wide\t: "+getWideArea());
System.out.println("Surround: "+getSurroundArea());
}
System.out.println();
}
}
public class MyGeometric {
int Choice=0;
Quadrangle Obj[]=new Quadrangle[10];
String alfabet="ABCDEFGHIJ";
int n;
void Menu(){
System.out.println("============= Menu =============");
System.out.println("Press 1 to Create a Rectangle");
System.out.println("Press 2 to Create a Square");
System.out.println("Press 3 to Print All Object");
System.out.println("Press 4 to Quit");
System.out.println("================================");
System.out.println("nb. You may only create maximum 10 Objects");
System.out.println();
System.out.print("Your Choice : ");
}
public static void main(String[] args) {
MyGeometric mg=new MyGeometric();
Scanner s=new Scanner(System.in);
String nm;
do{
mg.Menu();
try{
mg.Choice=s.nextInt();
switch(mg.Choice){
case 1:
mg.Obj[mg.n]=new Quadrangle();
mg.Obj[mg.n].setObject(String.valueOf(mg.alfabet.charAt(mg.n)), mg.Choice);
mg.n++;
break;
case 2:
mg.Obj[mg.n]=new Quadrangle();
mg.Obj[mg.n].setObject(String.valueOf(mg.alfabet.charAt(mg.n)), mg.Choice);
mg.n++;
break;
case 3:
for (int i = 0; i < mg.n; i++) {
mg.Obj[i].printObject();
}
break;
}
}catch(Exception e){
System.out.println(e.toString());
System.out.println("Your choice is wrong, please try again");
mg.Choice=0;
}
System.out.println();
}while(mg.Choice!=4);
System.out.println("Thank You");
}
}
(Rectangle.java)
package Geo1;
public class Rectangle {
protected double Long,Width;
public void setSize(double l,double w){
Long=l;
Width=w;
}
public double getWideArea(){
return Long*Width;
}
public double getSurroundArea(){
return 2*(Long+Width);
}
public void printObject(){
System.out.println("Long\t: "+Long);
System.out.println("Width\t: "+Width);
System.out.println("Wide\t: "+getWideArea());
System.out.println("Surround\t: "+getSurroundArea());
}
}
(Square.java)
package Geo1;
public class Square extends Rectangle {
protected double Side;
public void setSize(double s){
super.setSize(s, s);
Side=s;
}
@Override
public void printObject(){
System.out.println("Side\t: "+Side);
System.out.println("Wide\t: "+getWideArea());
System.out.println("Surround\t: "+getSurroundArea());
}
}
(MyGeometric.java)
package mygeometric;
import Geo1.*;
import java.util.Scanner;
class Quadrangle extends Square{
private String ObjName;
private int ObjType;
Scanner s=new Scanner(System.in);
public void setObject(String name,int t){
ObjType=t;
switch (ObjType){
case 1:
double l,w;
System.out.print("Long : ");
l=s.nextDouble();
System.out.print("Width : ");
w=s.nextDouble();
setSize(l, w);
ObjName="Rectangle " + name;
break;
case 2:
double sd;
System.out.print("Side : ");
sd=s.nextDouble();
setSize(sd);
ObjName="Square " + name;
break;
}
}
@Override
public void printObject(){
System.out.println("Data of " + ObjName );
if(ObjType==1){
System.out.println("Long\t: "+Long);
System.out.println("Width\t: "+Width);
System.out.println("Wide\t: "+getWideArea());
System.out.println("Surround: "+getSurroundArea());
}else if(ObjType==2){
System.out.println("Side\t: "+Side);
System.out.println("Wide\t: "+getWideArea());
System.out.println("Surround: "+getSurroundArea());
}
System.out.println();
}
}
public class MyGeometric {
int Choice=0;
Quadrangle Obj[]=new Quadrangle[10];
String alfabet="ABCDEFGHIJ";
int n;
void Menu(){
System.out.println("============= Menu =============");
System.out.println("Press 1 to Create a Rectangle");
System.out.println("Press 2 to Create a Square");
System.out.println("Press 3 to Print All Object");
System.out.println("Press 4 to Quit");
System.out.println("================================");
System.out.println("nb. You may only create maximum 10 Objects");
System.out.println();
System.out.print("Your Choice : ");
}
public static void main(String[] args) {
MyGeometric mg=new MyGeometric();
Scanner s=new Scanner(System.in);
String nm;
do{
mg.Menu();
try{
mg.Choice=s.nextInt();
switch(mg.Choice){
case 1:
mg.Obj[mg.n]=new Quadrangle();
mg.Obj[mg.n].setObject(String.valueOf(mg.alfabet.charAt(mg.n)), mg.Choice);
mg.n++;
break;
case 2:
mg.Obj[mg.n]=new Quadrangle();
mg.Obj[mg.n].setObject(String.valueOf(mg.alfabet.charAt(mg.n)), mg.Choice);
mg.n++;
break;
case 3:
for (int i = 0; i < mg.n; i++) {
mg.Obj[i].printObject();
}
break;
}
}catch(Exception e){
System.out.println(e.toString());
System.out.println("Your choice is wrong, please try again");
mg.Choice=0;
}
System.out.println();
}while(mg.Choice!=4);
System.out.println("Thank You");
}
}
Okay guys I will tell more about my project. As like as you know that the Square object is a Rectangle object that has the same side. So we can identify the Square as a child of Rectangle. This concept was called by Inheritance. So we can get the wide area of a square by using the method getWideArea() in Rectangle class.As well the surround area too.
May be we can find a wide area of square by square of side length. But in OOP concept we can reuse attribut and method of a class which we have previously made.
You can copy paste the source code free and if you will to download full source code you can go to link below and please give me a comment if you are a good reader.
0 comments:
Post a Comment
Please leave a comment