Add assignment-2 files

parent 24e79f33
......@@ -22,6 +22,20 @@
"RunOnceActivity.ShowReadmeOnStart": "true"
}
}]]></component>
<component name="RunManager">
<configuration name="Main" type="Application" factoryName="Application" temporary="true" nameIsGenerated="true">
<option name="MAIN_CLASS_NAME" value="Main" />
<module name="Assignment_2" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<recent_temporary>
<list>
<item itemvalue="Application.Main" />
</list>
</recent_temporary>
</component>
<component name="SpellCheckerSettings" RuntimeDictionaries="0" Folders="0" CustomDictionaries="0" DefaultDictionary="application-level" UseSingleDictionary="true" transferred="true" />
<component name="TaskManager">
<task active="true" id="Default" summary="Default task">
......
// Using method overloading.
public class AreaLoad {
// Calculate area for circle.
public double calculateArea(double x) {
return 3.14 * x * x;
}
// Calculate area for rectangle.
public double calculateArea(float x, double y) {
return x * y;
}
// Calculate area for triangle.
public double calculateArea(float x, float y, float constant) {
return (float) constant * x * y;
}
}
abstract class AreaRide {
abstract double calculateArea();
}
class Circle extends AreaRide{
double x;
Circle(double x) {
this.x = x;
}
@Override
double calculateArea() {
return 3.14 * x * x;
}
}
class Employee extends Person {
public String empID;
public String name;
Employee(String name, String empID) {
this.empID = empID;
this.name = name;
}
public void introduce() {
System.out.println("My name is: " + name + ". My employee id is " + empID);
}
public void work() {
System.out.println("Employee is working");
}
}
public class Main {
public static void main(String[] args) {
// Part1. Implement method overloading.
AreaLoad circle = new AreaLoad();
System.out.println(circle.calculateArea(3));
AreaLoad triangle = new AreaLoad();
System.out.println(triangle.calculateArea(4,4, 0.5f));
AreaLoad rectangle = new AreaLoad();
System.out.println(rectangle.calculateArea(3,2));
// Part 2. Implement method overriding.
Circle myCircle = new Circle(3);
System.out.println(myCircle.calculateArea());
Triangle myTriangle = new Triangle(4, 4, 0.5);
System.out.println(myTriangle.calculateArea());
Rectangle myRectangle = new Rectangle(3, 2);
System.out.println(myRectangle.calculateArea());
// Part 3. Implement abstraction
Employee myEmployee = new Employee("Qadeer", "HF@123123");
myEmployee.introduce();
myEmployee.work();
}
}
abstract class Person {
abstract void introduce();
abstract void work();
}
class Rectangle extends AreaRide{
double x;
double y;
Rectangle(double x, double y) {
this.x = x;
this.y = y;
}
@Override
double calculateArea() {
return x * y;
}
}
\ No newline at end of file
class Triangle extends AreaRide{
double x;
double y;
double constant;
Triangle(double x, double y, double constant) {
this.x = x;
this.y = y;
this.constant = constant;
}
@Override
double calculateArea() {
return constant * x * y;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment