COMP1406 Assignment #2 Due Friday, November 12 at 11:59 pm COMP1006/1406 – Fall 2021 Submit your five .java files to brightspace. There are 30 possible marks. 1 Temperature [10 marks] 10 Marks : All...

1 answer below »
Can you complete the Temperature class and complete the other classes in problem 2. Write the problems using a text editor known as Visual Studio Code. Send the solution in java file extension


COMP1406 Assignment #2 Due Friday, November 12 at 11:59 pm COMP1006/1406 – Fall 2021 Submit your five .java files to brightspace. There are 30 possible marks. 1 Temperature [10 marks] 10 Marks : All marks for this problem are correctness marks. Complete the provided Temperature class. Add any attributes and helper methods as needed but keep in mind that testing will involve only the methods you are asked to write/complete. You must complete the constructors and methods in the provided class (without changing any signatures, return types, or modifiers). In this problem you will need to be able to convert temperatures between Celsius, Fahrenheit and Kelvin. For help, see https://en.wikipedia.org/wiki/Conversion_of_units_of_temperature A Temperature object holds a single temperature and displays it in one of the three scales. Once a scale has been set, it will display the temperature in that scale until changed. The default scale is Celsius if not specified. The three scales are represented by an Enum class (Scale.java). For this assignment, the pur- pose of the enum class is to provide a consistent naming scheme for the different scales. Essentially, we assign fixed names for the three scales and use these everywhere in the code much like we would if we declared static final strings in our class. The compiler checks if the names are right. If we used Strings instead, and if you spelled the scale incorrectly (easy to do with these words), you would fail all the auto-testing. With enums, if you spell them incorrectly the code won’t compile letting you know. How do you use an enum? They are simple to use. You do not create them with the new operator and you do not need to use .equals to compare them. You just use them as place-holders for the value we want. (Also, see Tutorial 4.) Scale s = Scale.CELCIUS; // a variable of an enum type Temperature t1 = new Temperature(33.1, s); Temperature t2 = new Temperature(87.2, Scale.FAHRENHEIT); boolean b = Scale.CELCIUS == s; // will be true! can compare with == For one of the constructors, you are allowed to pass a string to specify the scale. It should allow for some flexibility in the input by accepting any prefix (in any case) of one of the valid temperature names. For example, ”FAHRENHEIT”, ”Fahr”, ”f” and ”FaHReNHeI” should be all accepted and interpreted as Fahrenheit (Scale.FAHRENHEIT). Inputs that are not valid prefixes must result in a created object that has temperature 0 Kelvin (using the scale Kelvin). So, ”FAR”, ”Selcius” and ”Kalvin” would create this zero Kelvin object. Some examples of using a Temperature object: Temperature t = new Temperature(10.1); System.out.println(t.getScale()); // outputs Scale.CELSIUS.toString() System.out.println(t); // outputs 10.1C t.setScale(Scale.FAHRENHEIT); // change scale System.out.println(t); // outputs 50.18F COMP1006/1406 - Fall 2021 1 https://en.wikipedia.org/wiki/Conversion_of_units_of_temperature Assignment #2 Due Friday, November 12 at 11:59 pm System.out.println(t.getScale()); // outputs Scale.FAHREHEIT.toString() t = new Temperature(12.25, "Kel"); // must recognize short form System.out.println(t); // outputs 12.25K t = new Temerature(99.9, "Far"); // bad input! System.out.println(t); // outputs 0.0K Note: You should have no static attributes or methods in your class (unless they were supplied in the starter code; or unless you have a main method for your own testing). A program called SimpleTemperatureProgram is provided with the code shown above that you can use as a starting point for your own testing if you wish. Submit your Temperature.java file to brightspace along with your .java files. 2 Shapes [20 marks] 20 Marks : All marks for this problem are correctness marks. In this problem, you are given an abstract Shape class and will need to complete four concrete subclasses of it. All shapes have an (x,y) coordinate that anchors the shape in the x-y plane. The XYCoord class is used to store (x,y) coordinates. All shapes can compute their own area and circumference1. Each of the concrete classes will have a constructor that is appropriate for the particular shape detailed as follows: public Rectangle(XYCoord anchor, double length, double width) public Square(XYCoord anchor, double length) // a square’s length and width are the same public Triangle(XYCoord p1, XYCoord p2, XYCOord p3) // p1, p2, p3 are the three coordinates of the corners of the triangle // p1 is also the anchor coordinate for this shape public Circle(XYCoord center, double radius) // center is also the anchor coordinate 1While circumference is usually used for a circle and perimeter is used for rectangles and triangles, we use the word circumference for any shape. It is the total length of the shape’s outline. COMP1006/1406 - Fall 2021 2 Assignment #2 Due Friday, November 12 at 11:59 pm You can use the provided ShapeExampleApp to help test your code. Do NOT change or submit the provided Shape.java file. When testing, we will use the version that was provided with the assignment. Be sure to make all FOUR concrete classes as the example app (and the testing code) will not compile unless all FOUR classes are present. Submit your Rctangle.java, Square.java, Triangle.java and Circle.java files to brightspace along with your other .java file. Submission Recap A complete assignment will consist of a FIVE files: Temperature.java, Rectangle.java, Square.java, Triangle.java and Circle.java. Brightspace will delete your previous submissions. Be sure to submit ALL of your files each time you submit. COMP1006/1406 - Fall 2021 3 Temperature [10 marks] Shapes [20 marks]
Answered 1 days AfterNov 07, 2021

Answer To: COMP1406 Assignment #2 Due Friday, November 12 at 11:59 pm COMP1006/1406 – Fall 2021 Submit your...

Shubham Kumar answered on Nov 09 2021
133 Votes
assingment/Circle.java
assingment/Circle.java
package assingment;
public class Circle extends Shape{
double radius;
    public Circle(XYCoord xyCoord, double d) {
        super(xyCoord);
radius=d;
    }
    @Override
    public double area() {
        // TODO Auto-generated method stub
        return (3.1415926535897*radius*radius) ;

    }
    @Override
    p
ublic double circumference() {
        return (3.1415926535897*2*radius) ;
    }
}
assingment/Rectangle.java
assingment/Rectangle.java
package assingment;
public class Rectangle extends Shape {
    XYCoord rec;
    double length,width;
    public Rectangle(double x, double y) {
        super(x, y);
        length=x;
        width=y;
    }
    public Rectangle(XYCoord a,double x, double y) {
        super(x, y);
        length=x;
        width=y;
        rec=new XYCoord(a.ab,a.or);
    }
    @Override
    public double area() {

        return length*width;
    }
    @Override
    public double circumference() {
        return 2*(length+width);

    }
}
assingment/Scale.java
assingment/Scale.java
package assingment;
public enum Scale {
    CELSIUS, FAHRENHEIT, KELVIN, NONE;
}
assingment/Shape.java
assingment/Shape.java
package assingment;
public abstract class Shape{
   // anchor coordinates of the shape
   protected final XYCoord anchor;


   // even though this is an abstract class it still
   // has constructors and at least one of them is used
   // whenever a child class is instantiated
   public Shape(double x, double y){
      this( new XYCoord(x,y) );
   }
   public Shape(XYCoord anchor){
      this.anchor = anchor;
   }
   /** Computes and returns the area of this shape. 
    * 
    * @return the area of this shape. 
    */
   public abstract double area();
   /** Computes and returns the circumference of this shape.
    * 
    * @return the circumference of this shape. 
    */
   public abstract double circumference();
   @Override
   public String toString(){
      return "Shape at " + this.anchor.toString();
   }
}
assingment/ShapeExampleApp.java
assingment/ShapeExampleApp.java
package assingment;
public class ShapeExampleApp{
 
    public static double epsilon = 0.00001;
    public static boolean eq(double x, double y){
        return Math.abs(x-y) < epsilon;
    }
    public static void out(Object o){
        System.out.println(o.toString());
    }

    public static void main(String[] args){
        out("Simple Shape Example Program");
        out("----------------------------");
        XYCoord point = new XYCoord(1.0,1.0);
        // make a rectangle shape
        Shape s = new Rectangle(point, 10, 25);
        out("\nRectangle!");
        out(s.toString());
        out("Rectangle's area = " + s.area());
        out("...expecting 250.0 : " + eq(250,s.area()));
        out("Rectangle's circumference = " + s.circumference());
        out("...expecting 70.0 : " + eq(70,s.circumference()));
        // make a square
        s = new Square(new XYCoord(0,0), 25.5);
        out("\nSquare!");
        out(s.toString());
        out("Square's area = " + s.area());
        out("...expecting 650.25 : " + eq(650.25,s.area()));
        out("Square's circumference = " + s.circumference());
        out("...expecting 102.0 : " + eq(102,s.circumference()));
        out(".is Square a Shape     :" + (s instanceof Shape));
        out(".is Square a Rectangle :" + (s instanceof Rectangle));
        out(".is Square a Square    :" + (s instanceof Square));

        // make a circle
        s = new Circle(new XYCoord(3,1.5), 7.2);
        out("\nCircle!");
        out(s.toString());
        out("Circle's area = " + s.area());
        out("...expecting...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here