Answer To: 1 COLLEGE OF ENGINEERING AND APPLIED SCIENCES DEPARTMENT OF COMPUTER SCIENCE ICSI201 Introduction to...
Vaishnavi R answered on May 02 2021
New folder/greynodes assignment/.classpath
New folder/greynodes assignment/.project
greynodes assignment
org.eclipse.jdt.core.javabuilder
org.eclipse.jdt.core.javanature
New folder/greynodes assignment/.settings/org.eclipse.jdt.core.prefs
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=15
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=15
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
org.eclipse.jdt.core.compiler.release=enabled
org.eclipse.jdt.core.compiler.source=15
New folder/greynodes assignment/bin/Circle.class
New folder/greynodes assignment/bin/Driver.class
New folder/greynodes assignment/bin/HelperClass.class
New folder/greynodes assignment/bin/InvalidTriangleException.class
New folder/greynodes assignment/bin/Rectangle.class
New folder/greynodes assignment/bin/Shape.class
New folder/greynodes assignment/bin/shapes.txt
Rectangle
red rectangle
12.0
34.0
Rectangle
square
12.0
12.0
Triangle
right triangle
3.0
4.0
5.0
Triangle
right triangle
3.0
4.0
1.0
Circle
red circle
2.5
Rectangle
blue rectangle
12.5
34.6
Rectangle
square
11.0
11.0
Triangle
right triangle
4.0
3.0
5.0
Triangle
right triangle
3.0
1.0
4.0
Circle
blue circle
4.5
Rectangle
white rectangle
15.0
30.5
Rectangle
square
15.0
15.0
Triangle
right triangle
3.0
5.0
4.0
Triangle
right triangle
1.0
4.0
3.0
Circle
red circle
7.5
New folder/greynodes assignment/bin/Triangle.class
New folder/greynodes assignment/src/Circle.java
New folder/greynodes assignment/src/Circle.java
public class Circle extends Shape {
private double radius;
double pi = 3.14;
public Circle() {
this.name = "Circle";
this.radius = 1.0;
}
// Initializes new object with name and its variables with suitable value
public Circle(String name, double r) {
this.name = name;
this.radius = r;
}
// returns value of radius
public double getRadius() {
return radius;
}
// sets value of radius
public void setRadius(double radius) {
this.radius = radius;
}
// returns area of circle
@Override
public double area() {
return 2*pi*this.radius;
}
// checks if objects are equal and returns true if they are
@Override
public boolean equals(Object o) {
if (o == this) {
return true;
}
if (!(o instanceof Circle)) {
return false;
}
return true;
}
// return attributes and method values of circle
@Override
public String toString() {
return String.format(this.name + " with radius " + this.radius +" area is "+ this.area());
}
}
New folder/greynodes assignment/src/Driver.java
New folder/greynodes assignment/src/Driver.java
import java.io.FileNotFoundException;
public class Driver {
public static void main(String[] args) throws FileNotFoundException {
HelperClass hc = new HelperClass();
hc.start();
}
}
New folder/greynodes assignment/src/HelperClass.java
New folder/greynodes assignment/src/HelperClass.java
import java.io.*;
import java.util.*;
public class HelperClass {
public static void display(ArrayList shapes) {
for(int i=0; i {
System.out.println(shapes.get(i).toString());
}
}
public static void start() throws FileNotFoundException {
ArrayList shapes= new ArrayList();
shapes = create(shapes);
display(shapes);
}
public static ArrayList create(ArrayList shapes) throws FileNotFoundException{
String path = "E:\\programs\\eclipse\\ecplise-workspace\\greynodes assignment\\src\\shapes.txt";
try (Scanner fileScan = new Scanner(
new FileReader(new File(path)))) {
while(fileScan.hasNext()) {
String type = fileScan.nextLine();
if(type.equals("Rectangle")) {
String name = fileScan.nextLine();
double l = fileScan.nextDouble();
double w = fileScan.nextDouble();
// System.out.println("rect found");
Rectangle r = new Rectangle(name,l,w);
shapes.add(r);
}
else if(type.equals("Circle")) {
String name = fileScan.nextLine();
double r = fileScan.nextDouble();
// System.out.println("circle found");
Circle c = new Circle(name,r);
shapes.add(c);
}
else if(type.equals("Triangle")) {
String name = fileScan.nextLine();
double s1 = fileScan.nextDouble();
double s2 = fileScan.nextDouble();
double s3 = fileScan.nextDouble();
// System.out.println("triangle found");
Triangle t = new Triangle(name,s1,s2,s3);
shapes.add(t);
}
}
}
catch (IOException ex){
ex.printStackTrace();
}
return shapes;
}
}
New folder/greynodes assignment/src/InvalidTriangleException.java
New folder/greynodes assignment/src/InvalidTriangleException.java
public class InvalidTriangleException extends Exception{
public InvalidTriangleException(String msg){
super(msg);
}
}
New folder/greynodes assignment/src/Rectangle.java
New folder/greynodes assignment/src/Rectangle.java
public class Rectangle extends Shape {
private double length;
private double width;
// Initializes new object with name and its variables with suitable value
public Rectangle() {
this.name = "Rectangle";
this.length = 1.0;
this.width = 1.0;
}
// Initializes new object with name and its variables with suitable value
public Rectangle(String name, double length, double width) {
this.name = name;
this.length = length;
this.width = width;
}
// return length of rectangle
public double getLength() {
return length;
}
// sets length of rectangle
public void setLength(int length) {
this.length = length;
}
// return width of rectangle
public double getWidth() {
return width;
}
// sets width of rectangle
public void setWidth(int width) {
this.width = width;
}
// return area of rectangle
@Override
public double area() {
return length * width;
}
// checks if objects are equal and returns true if they are
@Override
public boolean equals(Object o) {
if (o == this) {
return true;
}
if (!(o instanceof Rectangle)) {
return false;
}
return true;
}
// return attributes and method values of rectangle
@Override
public String toString() {
return String.format(this.name + " with length " + this.length + " and width " + this.width +" area is "+ this.area());
}
}
New folder/greynodes assignment/src/Shape.java
New folder/greynodes assignment/src/Shape.java
public abstract class Shape {
String name;
// Initializes new object with name and its variables with suitable value
Shape() {
this.name = "shape";
}
// returns name of shape object
public String getName() {
return this.name;
}
// sets name of shape object
public void setName(String name) {
this.name = name;
}
public abstract double area();
// checks if objects are equal and returns true if they are
@Override
public boolean equals(Object o) {
if (o == this) {
return true;
}
if (!(o instanceof Shape)) {
return false;
}
return true;
}
// return attributes of shape class
@Override
public String toString() {
return String.format(name);
}
}
New folder/greynodes assignment/src/shapes.txt
Rectangle
red rectangle
12.0
34.0
Rectangle
square
12.0
12.0
Triangle
right triangle
3.0
4.0
5.0
Triangle
right triangle
3.0
4.0
1.0
Circle
red circle
2.5
Rectangle
blue rectangle
12.5
34.6
Rectangle
square
11.0
11.0
Triangle
right triangle
4.0
3.0
5.0
Triangle
right triangle
3.0
1.0
4.0
Circle
blue circle
4.5
Rectangle
white rectangle
15.0
30.5
Rectangle
square
15.0
15.0
Triangle
right triangle
3.0
5.0
4.0
Triangle
right triangle
1.0
4.0
3.0
Circle
red circle
7.5
New folder/greynodes assignment/src/Triangle.java
New folder/greynodes assignment/src/Triangle.java
public class Triangle extends Shape {
private double sideOne;
private double sideTwo;
private double sideThree;
// Initializes new object with name and its variables with suitable value
public Triangle() {
this.name = "Triangle";
this.sideOne = 1.0;
this.sideTwo = 1.0;
this.sideThree = 1.0;
}
// Initializes new object with name and its variables with suitable value
public Triangle(String name, double s1, double s2, double s3) {
this.name = name;
try {
if (isValid(s1, s2, s3)) {
this.sideOne = s1;
this.sideTwo = s2;
this.sideThree = s3;
} else {
throw new InvalidTriangleException("triangle not possible");
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
// return side of triangle
public double getSideOne() {
return sideOne;
}
// sets side of triangle
public void setSideOne(double sideOne) {
try {
if (isValid(sideOne, this.sideTwo, this.sideThree)) {
this.sideOne = sideOne;
} else {
throw new InvalidTriangleException("triangle not possible");
}
} catch (Exception e) {
e.printStackTrace();
}
}
// return side of triangle
public double getSideTwo() {
return sideTwo;
}
// sets side of triangle
public void setSideTwo(double sideTwo) {
try {
if (isValid(this.sideOne, sideTwo, this.sideThree)) {
this.sideTwo = sideTwo;
} else {
throw new InvalidTriangleException("triangle not possible");
}
} catch (Exception e) {
e.printStackTrace();
}
}
// return side of triangle
public double getSideThree() {
return sideThree;
}
// sets side of triangle
public void setSideThree(double sideThree) {
try {
if (isValid(this.sideOne, this.sideTwo, sideThree)) {
this.sideThree = sideThree;
} else {
throw new InvalidTriangleException("triangle not possible");
}
} catch (Exception e) {
e.printStackTrace();
}
}
// return area of triangle
@Override
public double area() {
double p = (this.sideOne + this.sideTwo + this.sideThree) / 2;
return Math.sqrt(p * (p - this.sideOne) * (p - this.sideTwo) * (p - this.sideThree));
}
// checks if valid triangle is possible with given sides, returns true if it is possible
public boolean isValid(double s1, double s2, double s3) {
if (((s1 + s2) <= s3) || ((s3 + s2) <= s1) || ((s1 + s3) <= s2)) {
return false;
}
return true;
}
// return attributes and method values of triangle
@Override
public String toString() {
return String.format(this.name + " with sides" + this.sideOne + ", "+this.sideTwo + ", "+ this.sideThree +" area is "+ this.area());
}
// checks if objects are equal and returns true if they are
@Override
public boolean equals(Object o) {
if(o==this)
{
return true;
}
if(!(o instanceof Triangle)) {
return false;
}
return true;
}
}
New folder/uml.mdj
{
"_type": "Project",
"_id": "AAAAAAFF+h6SjaM2Hec=",
"name": "Untitled",
"ownedElements": [
{
"_type": "UMLModel",
"_id": "AAAAAAFF+qBWK6M3Z8Y=",
"_parent": {
"$ref": "AAAAAAFF+h6SjaM2Hec="
},
"name": "Model",
"ownedElements": [
{
"_type": "UMLClassDiagram",
"_id": "AAAAAAFF+qBtyKM79qY=",
"_parent": {
"$ref": "AAAAAAFF+qBWK6M3Z8Y="
},
"name": "Main",
"defaultDiagram": true,
"ownedViews": [
{
"_type": "UMLClassView",
"_id": "AAAAAAF5LHi7W4hPoWM=",
"_parent": {
"$ref": "AAAAAAFF+qBtyKM79qY="
},
"model": {
"$ref": "AAAAAAF5LHi7WohN2RU="
},
"subViews": [
{
"_type": "UMLNameCompartmentView",
"_id": "AAAAAAF5LHi7XIhQEXw=",
"_parent": {
"$ref": "AAAAAAF5LHi7W4hPoWM="
},
"model": {
"$ref": "AAAAAAF5LHi7WohN2RU="
},
"subViews": [
{
"_type": "LabelView",
"_id": "AAAAAAF5LHi7XIhRxiw=",
"_parent": {
"$ref": "AAAAAAF5LHi7XIhQEXw="
},
"visible": false,
"font": "Arial;13;0",
"left": 240,
"top": -224,
"height": 13
},
{
"_type": "LabelView",
"_id": "AAAAAAF5LHi7XIhSatw=",
"_parent": {
"$ref": "AAAAAAF5LHi7XIhQEXw="
},
"font": "Arial;13;3",
"left": 245,
"top": 31,
"width": 135,
"height": 13,
"text": "Shape"
},
{
"_type": "LabelView",
"_id": "AAAAAAF5LHi7XIhTYfc=",
"_parent": {
"$ref": "AAAAAAF5LHi7XIhQEXw="
},
"visible": false,
"font": "Arial;13;0",
"left": 240,
"top": -224,
"width": 73.67724609375,
"height": 13,
"text": "(from Model)"
},
{
"_type": "LabelView",
"_id": "AAAAAAF5LHi7XIhUxv8=",
"_parent": {
"$ref": "AAAAAAF5LHi7XIhQEXw="
},
"visible": false,
"font": "Arial;13;0",
"left": 240,
"top": -224,
"height": 13,
"horizontalAlignment": 1
}
],
"font": "Arial;13;0",
"left": 240,
"top": 24,
"width": 145,
"height": 25,
"stereotypeLabel": {
"$ref": "AAAAAAF5LHi7XIhRxiw="
},
"nameLabel": {
"$ref": "AAAAAAF5LHi7XIhSatw="
},
"namespaceLabel": {
"$ref": "AAAAAAF5LHi7XIhTYfc="
},
"propertyLabel": {
"$ref": "AAAAAAF5LHi7XIhUxv8="
}
},
{
"_type": "UMLAttributeCompartmentView",
"_id": "AAAAAAF5LHi7XIhVT+k=",
"_parent": {
"$ref": "AAAAAAF5LHi7W4hPoWM="
},
"model": {
"$ref": "AAAAAAF5LHi7WohN2RU="
},
"subViews": [
{
"_type": "UMLAttributeView",
"_id": "AAAAAAF5LHkZJYh6gbM=",
"_parent": {
"$ref": "AAAAAAF5LHi7XIhVT+k="
},
"model": {
"$ref": "AAAAAAF5LHkY74h3sRI="
},
"font": "Arial;13;0",
"left": 245,
"top": 54,
"width": 135,
"height": 13,
"text": "-name",
"horizontalAlignment": 0
},
{
"_type": "UMLAttributeView",
"_id": "AAAAAAF5LHnqyIiDAhM=",
"_parent": {
"$ref": "AAAAAAF5LHi7XIhVT+k="
},
"model": {
"$ref": "AAAAAAF5LHnqtYiAtDc="
},
"visible": false,
"font": "Arial;13;0",
"left": 365,
"top": -43,
"width": 134,
"height": 13,
"text": "+Attribute1",
"horizontalAlignment": 0
},
{
"_type": "UMLAttributeView",
"_id": "AAAAAAF5LIl4i4+C0vU=",
"_parent": {
"$ref": "AAAAAAF5LHi7XIhVT+k="
},
"model": {
"$ref": "AAAAAAF5LIl4gI90gwA="
},
"visible": false,
"font": "Arial;13;0",
"left": 245,
"top": 69,
"width": 135,
"height": 13,
"text": "+Port1",
"horizontalAlignment": 0
}
],
"font": "Arial;13;0",
"left": 240,
"top": 49,
"width": 145,
"height": 23
},
{
"_type": "UMLOperationCompartmentView",
"_id": "AAAAAAF5LHi7XIhW5Ls=",
"_parent": {
"$ref": "AAAAAAF5LHi7W4hPoWM="
},
"model": {
"$ref": "AAAAAAF5LHi7WohN2RU="
},
"subViews": [
{
"_type": "UMLOperationView",
"_id": "AAAAAAF5LIBSt4kgKTM=",
"_parent": {
"$ref": "AAAAAAF5LHi7XIhW5Ls="
},
"model": {
"$ref": "AAAAAAF5LIBSfokdSjM="
},
"font": "Arial;13;2",
"left": 245,
"top": 77,
"width": 135,
"height": 13,
"text": "+area()",
"horizontalAlignment": 0
},
{
"_type": "UMLOperationView",
"_id": "AAAAAAF5LID3L4kpsUI=",
"_parent": {
"$ref": "AAAAAAF5LHi7XIhW5Ls="
},
"model": {
"$ref": "AAAAAAF5LID3HokmFDM="
},
"font": "Arial;13;0",
"left": 245,
"top": 92,
"width": 135,
"height": 13,
"text": "+getName()",
"horizontalAlignment": 0
},
{
"_type": "UMLOperationView",
"_id": "AAAAAAF5LIEyKYkw7ys=",
"_parent": {
"$ref": "AAAAAAF5LHi7XIhW5Ls="
},
"model": {
"$ref": "AAAAAAF5LIEyIIktxxs="
},
"font": "Arial;13;0",
"left": 245,
"top": 107,
"width": 135,
"height": 13,
"text": "+setName()",
"horizontalAlignment": 0
},
{
"_type": "UMLOperationView",
"_id": "AAAAAAF5LIF2Cok3J10=",
"_parent": {
"$ref": "AAAAAAF5LHi7XIhW5Ls="
},
"model": {
"$ref": "AAAAAAF5LIF13ok0WEE="
},
"font": "Arial;13;0",
"left": 245,
"top": 122,
"width": 135,
"height": 13,
"text": "+equals()",
"horizontalAlignment": 0
},
{
"_type": "UMLOperationView",
"_id": "AAAAAAF5LIGiwok+Yz0=",
"_parent": {
"$ref": "AAAAAAF5LHi7XIhW5Ls="
},
"model": {
"$ref": "AAAAAAF5LIGiook7dK8="
},
"font": "Arial;13;0",
"left": 245,
"top": 137,
"width": 135,
"height": 13,
"text": "+toString()",
"horizontalAlignment": 0
}
],
"font": "Arial;13;0",
"left": 240,
"top": 72,
"width": 145,
"height": 83
},
{
"_type": "UMLReceptionCompartmentView",
"_id": "AAAAAAF5LHi7XIhXadI=",
"_parent": {
"$ref": "AAAAAAF5LHi7W4hPoWM="
},
"model": {
"$ref": "AAAAAAF5LHi7WohN2RU="
},
"visible": false,
"font": "Arial;13;0",
"left": 120,
"top": -112,
"width": 10,
"height": 10
},
{
"_type": "UMLTemplateParameterCompartmentView",
"_id": "AAAAAAF5LHi7XIhY0C8=",
"_parent": {
"$ref": "AAAAAAF5LHi7W4hPoWM="
},
"model": {
"$ref": "AAAAAAF5LHi7WohN2RU="
},
"visible": false,
"font": "Arial;13;0",
"left": 120,
"top": -112,
"width": 10,
"height": 10
}
],
"font": "Arial;13;0",
"containerChangeable": true,
"left": 240,
"top": 24,
"width": 145,
"height": 146,
"nameCompartment": {
"$ref": "AAAAAAF5LHi7XIhQEXw="
},
"attributeCompartment": {
"$ref": "AAAAAAF5LHi7XIhVT+k="
},
"operationCompartment": {
"$ref": "AAAAAAF5LHi7XIhW5Ls="
},
"receptionCompartment": {
"$ref": "AAAAAAF5LHi7XIhXadI="
},
"templateParameterCompartment": {
"$ref": "AAAAAAF5LHi7XIhY0C8="
}
},
{
"_type": "UMLClassView",
"_id": "AAAAAAF5LX9BM5D2+NU=",
"_parent": {
"$ref": "AAAAAAFF+qBtyKM79qY="
},
"model": {
"$ref": "AAAAAAF5LX9BLpD0wo8="
},
"subViews": [
{
"_type": "UMLNameCompartmentView",
"_id": "AAAAAAF5LX9BNJD3GuY=",
"_parent": {
"$ref": "AAAAAAF5LX9BM5D2+NU="
},
"model": {
"$ref": "AAAAAAF5LX9BLpD0wo8="
},
"subViews": [
{
"_type": "LabelView",
"_id": "AAAAAAF5LX9BNJD4H0M=",
"_parent": {
"$ref": "AAAAAAF5LX9BNJD3GuY="
},
"visible": false,
"font": "Arial;13;0",
"left": -256,
"top": 178,
"height": 13
},
{
"_type": "LabelView",
"_id": "AAAAAAF5LX9BNJD5qFc=",
"_parent": {
"$ref": "AAAAAAF5LX9BNJD3GuY="
},
"font": "Arial;13;1",
"left": 117,
"top": 303,
"width": 151,
"height": 13,
"text": "Triangle"
},
{
"_type": "LabelView",
"_id":...