- Added remaining classes.
	- Added abstract class shape.
	- _Area Interface.
	- Lab test program.
This commit is contained in:
HeshamTB 2020-09-26 06:16:00 +03:00
parent 187f2cd313
commit ad76b6be55
Signed by: Hesham
GPG Key ID: 74876157D199B09E
7 changed files with 221 additions and 4 deletions

124
lab-02/.idea/uiDesigner.xml Normal file
View File

@ -0,0 +1,124 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Palette2">
<group name="Swing">
<item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" />
</item>
<item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" />
</item>
<item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.png" removable="false" auto-create-binding="false" can-attach-label="true">
<default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" />
</item>
<item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" />
<initial-values>
<property name="text" value="Button" />
</initial-values>
</item>
<item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="RadioButton" />
</initial-values>
</item>
<item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="CheckBox" />
</initial-values>
</item>
<item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" />
<initial-values>
<property name="text" value="Label" />
</initial-values>
</item>
<item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" />
</item>
<item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" />
</item>
<item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1">
<preferred-size width="-1" height="20" />
</default-constraints>
</item>
<item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" />
</item>
<item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" />
</item>
</group>
</component>
</project>

22
lab-02/src/Circle.java Normal file
View File

@ -0,0 +1,22 @@
public class Circle extends _Shape {
private double radius;
public Circle(double radius){
super("Circle");
setRadius(radius);
}
public void setRadius(double radius){
if (radius >= 0) this.radius = radius;
else this.radius = 1;
}
public double getRadius(){ return radius; }
public double getArea() { return Math.PI*Math.pow(radius, 2); }
public String toString(){
return String.format("%s%.2f\n", super.toString(), this.getArea());
}
}

31
lab-02/src/Triangle.java Normal file
View File

@ -0,0 +1,31 @@
public class Triangle extends _Shape {
private double base;
private double height;
public Triangle(double base, double height){
super("Triangle");
setBase(base);
setHeight(height);
}
public void setBase(double base){
if (base != 0) this.base = base;
else this.base = 1;
}
public void setHeight(double height){
if (height != 0) this.height = height;
else this.height = 1;
}
public double getBase() { return this.base; }
public double getHeight() {return this.height; }
public double getArea() { return (this.base*this.height)/2; }
public String toString() {
return String.format("%s%.2f\n", super.toString(), this.getArea());
}
}

3
lab-02/src/_Area.java Normal file
View File

@ -0,0 +1,3 @@
public interface _Area {
public double getArea();
}

View File

@ -4,12 +4,12 @@ public class _Rectangle extends _Shape {
private double height; private double height;
public _Rectangle(double w){ public _Rectangle(double w){
this(w, w);
//Since only width or height is given, assume square. //Since only width or height is given, assume square.
setWidth(w);
setHeight(w);
} }
public _Rectangle(double w, double h){ public _Rectangle(double w, double h){
super("Rectangle");
setWidth(w); setWidth(w);
setHeight(h); setHeight(h);
} }
@ -32,6 +32,6 @@ public class _Rectangle extends _Shape {
public double getHeight(){ return this.height; } public double getHeight(){ return this.height; }
public String toString() { public String toString() {
return " ";//TODO: _Rectangle toString output format return String.format("%s%.2f\n", super.toString(), this.getArea());
} }
} }

View File

@ -1,9 +1,16 @@
public abstract class _Shape { public abstract class _Shape implements _Area {
private String name; private String name;
public _Shape(String name){ this.name = name; }
public void setName(String name) { this.name = name; } public void setName(String name) { this.name = name; }
public String getName(){ return name; } public String getName(){ return name; }
public String toString(){
return String.format("Class name is %s\n" +
"Area = ", this.name);
}
} }

30
lab-02/src/testLab2.java Normal file
View File

@ -0,0 +1,30 @@
public class testLab2 {
public static void main(String args[]){
_Shape[] shapes = new _Shape[3];
shapes[0] = new Circle(2.2);
shapes[1] = new Triangle(4.9, 7.43);
shapes[2] = new _Rectangle(4.5);
for (_Shape shape : shapes) {
System.out.println(shape.toString());
}
Circle c1 = new Circle(3.9);
((_Area)c1).getArea();
//_Shape s = new _Rectangle(4.1);
//_Area area = new _Area();
_Area[] areas = new _Area[2];
areas[0] = new _Rectangle(6.5);
areas[1] = new Circle(5.5);
_Shape shape2 = new Triangle(4.1, 7.3);
shape2.getName();
shape2.getArea();
//shape2.base;
_Shape r2 = new _Rectangle(8.94);
if (r2 instanceof _Rectangle) ((_Rectangle) r2).setHeight(5);
}
}