代做COMP1006/1406 – Fall 2024 Assignment #2代做留学生Java程序

2024-10-17 代做COMP1006/1406 – Fall 2024 Assignment #2代做留学生Java程序

COMP1006/1406 Fall 2024

Assignment #2

Due Monday, October 14th at 11:59 pm

Submit your .java files to Gradescope.

The entire assignment is out of 10.

This assignment will mostly (if not completely) be graded for correctness.

Be sure that your code compiles and runs. Test your code!

There is a 48-hour grace period in which submissions will be accepted without penalty. If need be, you can submit this assignment up to Wednesday, October 16th, at 11:59pm without any penalty. However, there will not be any office hours and no guarantees that questions will be answered on discord over the weekend. Be sure to start early and submit often.

Do NOT change the input types, output types or modifiers of the methods you are completing. If you change any of these, it will not compile when submitted.

Part One

Temperature [4 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).

A temperature consists of a value (magnitude) and a scale. For example, if the temperature is -17.4C, then its value  is -17.4 and its scale  is Celsius.  The valid scales that we will consider for our Temperature objects will be Celsius, Fahrenheit and Kelvin.  Once a scale has been set, a Temperature object will always display its temperature in that scale until the scale is changed. The default scale is Celsius if not specified.

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

The three scales are represented by Strings in the provided Scale class (static class attributes). For this assignment, the purpose of the Scale class is to provide a consistent naming scheme for the diferent scales. Essentially, we assign fixed names for the three scales and use these everywhere in the code (so that a simple spelling mistake in your code does not result in failing every test case).

Some examples of using a Temperature object:

Temperature  t  =  new  Temperature(10 .1);

System.out.println(t .getScale());

// displays  "CELSIUS"

System.out.println(t .toString());

// displays  "10 .100C" with  exactly  3 decimal digits

System.out.println(t .getValue());

// displays  "10 .1" with at least 1 decimal digit

t.setScale(Scale.FAHRENHEIT);

// change  scale

System.out.println(t .toString());

// displays  "50.180F" (notice it converted the value!)

System.out.println(t .getScale());

// displays  "FAHRENHEIT"

t  =  new  Temperature(12 .25,  "KELVIN");  //  scale  input  is  not  from  Scale!  SEE  DOCUMENTATION!

System.out.println(t .getScale());

//  displays  "NONE"

System.out.println(t .toString());

// displays  "0 .000N"

Note: You must provide the state (instance attributes) for the Temperature class. You must de- cide what state to store for this problem. But, you must only use instance attributes. You should have no static attributes in your class.

Note: Temperature values are floating point numbers. If an object’s value is expected to be 0.1 and your output (from getValue() is 0.09999999999998), that is OK. You are not asked to perform. any rounding in the getValue() method.

Note: The provided toString() method will always display your Temperature objects using three (3) decimal places. Rounding will  happen automatically with this method.

Note: 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.

Note: Do not change the Scale.java file at all. Do not submit the Scale.java file.

Include your Temperature.java file when you submit.

Part Two

UML Class Diagrams

If a class called Child extends a class called Parent, we can visualize this as follows:

The arrow goes from the child (descendent) class to the parent (ancestor) class.  The arrow head should be open.

In Java, if a class does not explicitly extends another class, it will automatically inherit from Java’s Object class. This is true for every class (except Object). So, the UML class diagram from above could also be constructed as

We will often leave out Object since we know it should always appear at he top of any class hierarchy that we have.

Shapes

In this problem, you are provided with a Shape class and will need to create the other four classes shown in the following class hierarchy.

Each shape object has one (x,y) coordinate that anchors  it in the x-y plane.  This anchor point is fixed and cannot change once an object is created.  The other parameters can change though. The provided XYCoord class is used to store (x,y) coordinates. All shapes must be able to compute their own area and perimeter in addition to specific methods (outlined below).  You are free to add any state  to your classes as needed but you should not be storing any information in a given object twice.

Each of the classes will have a constructor that is appropriate for the particular shape detailed as follows:

public  Quadrilateral(XYCoord  a,  XYCoord  b,  XYCoord  c,  XYCoord  d)

//  a  quadrilateral  has  four  straight  sides  and  four  corner  points  (vertices)

//  the  corner  points  are  specified  by  the  inputs  a,b,c,d

//  a  is  the  anchor  coordinate  for  this  shape .

public  Square(XYCoord  anchor,  double  length)

//  a  square  is  a  special  quadrilateral  (all  sides  have  the  same

//  length  and  the  angle  at  each  corner  is  90  degrees) .

//  the  anchor  is  the  bottom-left  corner  of  the  square  and  length  >=  0

public  Triangle(XYCoord  a,  XYCoord  b,  XYCoord  c)

//  a,b,c  are  the  three  coordinates  of  the  corners  (vertices)  of  the  triangle

//  a  is  the  anchor  coordinate  for  this  shape

public  Circle(XYCoord  centre,  double  radius)

//  centre  is  the  anchor  coordinate  and  radius  >=  0

You will also need to create the following setter  methods in the specified classes. Note that there is no setter for the Quadrilateral class.:

Circle  Class

public  void  setRadius(double  newRadius)

//  changes  the  object’s  radius  to  be  newRadius

Triangle  Class

public  void  setBC(XYCoord  newB,  XYCoord  newC)

//  changes  the  object’s  b  and  c  points  to  be  newB  and  newC

Square  Class

public  void  setLength(double  newLength)

//  changes  the  object’s  length  to  be  newLength

You can use the provided ShapeExampleApp to help test your code. Do NOT change or submit the provided Shape.java file. Do NOT change or submit the provided XYCoord.java file. When testing, we will use the files as provided (and delete any version you submit).

The area for the square and circle should be straightforward to compute. For the quadrilateral, note that you can think of this shape as being composed of two triangles next to each other. For a triangle, look at the Help.java file that is provide; or, you can use the following helpful resource: https://byjus.com/maths/area-of-a-triangle/

Include your Quadrilateral.java, Square.java, Triangle.java and

Circle.java files when you submit.

Note that the shapes will always be created with coordinates as shown below.  That is, the

ordering of the coordinates will correspond to the labelling on the diagrams.

1) You should have no static attributes in any of your classes.

2) Read the specifications in the skeleton files carefully if provided.

3) Be sure to use INFORMATION HIDING.


Part One: Do not use Strings that look like the attributes from Scale.

Use the actual static constants provided in the Scale class.


Part Two: The classes must use inheritance that follows the UML diagram provided!

Do NOT store the same data twice in your objects.