Question

Short Answer Questions Here is a Java interface for moving/resizing shapes. /** General interface to all kinds of geometric s

Short Answer Questions Q1. [10 marks] Implementa class called Circle that implements the MovableSizable interface, and adds a

Short Answer Questions Q2. [5 marks] Drawa UML class diagram of the MovableSizable interface and your Circle class, showing a

Short Answer Questions Q3. [5 marks] Write a JUnit 5.0 test class, that has two tests of your Circle class: 1) one test that

Short Answer Questions Q4. [10 marks] The following UML class diagram describes the structure of a Java program for managing

[Continued] Your Course class should have:

1)a getter for the course 'code', so that it is read-only;

2)a getter and setter for the lecturer attribute, so that it is readable and writable;

3)a constructor that creates the associations shown in the UML diagram;

4)an enrolStudent method that adds a given Student object into the 'enrolled' set;

5)a withdrawStudent method that removes a given Student object from the 'enrolled' set and returns true if the student was successfully found and removed, false otherwise.

Short Answer Questions Here is a Java interface for moving/resizing shapes. /** General interface to all kinds of geometric shapes. */ public interface MovableSizable /** Resize this shape. Example. 0.5 halves the size */ void resize(double scale); /** @return total height, rounded to the nearest pixel. */ intgetHeight); /** @return total width, rounded to the nearest pixel. */ int getWidth(); /**Move shape so it is centred on (xv). */ void move Tolint x, int y); int getX); int getX)
Short Answer Questions Q1. [10 marks] Implementa class called Circle that implements the MovableSizable interface, and adds a constructor that takes two int values (the initial x.y centre of the circle) and one double value (the initial radius of the circle). For example, new Circle(100,200,3.5) would create a new circle centred at position (100,200) with diameter 7.0 pixels. Note that the constructor must throw an llegalArgumentException if the radius is zero or negative.
Short Answer Questions Q2. [5 marks] Drawa UML class diagram of the MovableSizable interface and your Circle class, showing all the details of each class (e.g. methods, fields, constructors)
Short Answer Questions Q3. [5 marks] Write a JUnit 5.0 test class, that has two tests of your Circle class: 1) one test that creates a circle at position (0,0) with radius 3.6, and checks that all the getter methods return the correct values (width and height are both 7 pixels and the x and y locations are both 0). Then it calls moveTo(10,20) and resize(3.0) and checks all the getter methods again (x should be 10, y should be 20, and width and height should both be 22 pixels, because 3.6*3*2=21.6, which rounds up to 22) 2) one test that tries to create a circle with radius of -2.0. The test should pass if this throws an exception, and fail if the circle is constructed without any errors.
Short Answer Questions Q4. [10 marks] The following UML class diagram describes the structure of a Java program for managing university courses. Implement just the Course class by writing a Java class that implements it. You can assume that the Student and Staff classes have already been coded in Java and that they have getter methods for each data field. [see next page for more details] Course Student -id : int -code: String name: String +Student() +Course(code:String) enrolled enrolStudent(s:Student) +sitExamint +withdrawStudent(s: Student): boolean lecturer Staff name: String +Staff +markExams0
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hi, I have answered this question before. Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

// Circle.java

public class Circle implements MovableSizable {

      //fields

      private int x, y;

      private double radius;

     

      //constructor

      public Circle(int x, int y, double radius) {

            if (radius <= 0) {

                  //invalid radius, throwing IllegalArgumentException

                  throw new IllegalArgumentException();

            }

            //initializing values

            this.x = x;

            this.y = y;

            this.radius = radius;

      }

      @Override

      public void resize(double scale) {

            //resizing radius by given scale

            radius *= scale;

      }

      @Override

      public int getHeight() {

            //rounding height and returning it

            return (int) Math.round(radius*2);

      }

      @Override

      public int getWidth() {

            //rounding width and returning it

            return (int) Math.round(radius*2);

      }

      @Override

      public void moveTo(int x, int y) {

            //updating x and y

            this.x = x;

            this.y = y;

      }

      @Override

      public int getX() {

            return x;

      }

      @Override

      public int getY() {

            return y;

      }

}

//UML diagram

Circle x: int y: int radius: double T Movable Sizable resize(double) void Circle(int,int,double) getHeight()int resize(double

// CircleTest.java

import static org.junit.Assert.*;

import org.junit.Test;

public class CircleTest {

      @Test

      public void test1() {

            // creating a circle. testing getter methods

            Circle circle = new Circle(0, 0, 3.6);

            assertEquals(circle.getX(), 0);

            assertEquals(circle.getY(), 0);

            assertEquals(circle.getHeight(), 7);

            assertEquals(circle.getWidth(), 7);

            // testing moveTo and resize methods

            circle.moveTo(10, 20);

            circle.resize(3.0);

            assertEquals(circle.getX(), 10);

            assertEquals(circle.getY(), 20);

            assertEquals(circle.getHeight(), 22);

            assertEquals(circle.getWidth(), 22);

      }

      @Test

      public void test2() {

            // ensuring that IllegalArgumentException is thrown when a negative or

            // zero radius is given as argument

            try {

                  Circle circle = new Circle(0, 0, -2);

                  fail("No exception thrown. Was expecting IllegalArgumentException");

            } catch (IllegalArgumentException e) {

            } catch (Exception e) {

                  fail("Should throw IllegalArgumentException, got: " + e);

            }

      }

}

// Course.java

import java.util.ArrayList;

public class Course {

      // attributes

      private String code; // course code

      private ArrayList<Student> enrolled; // list of students enrolled

      private Staff lecturer; // lecturer

      // (3)a constructor that creates the associations shown in the UML diagram

      public Course(String code) {

            this.code = code;

            // initializing list

            enrolled = new ArrayList<Student>();

      }

      // 1)a getter for the course 'code', so that it is read-only;

      public String getCode() {

            return code;

      }

      // 2)a getter for the lecturer attribute, so that it is readable

      public Staff getLecturer() {

            return lecturer;

      }

      // 2)a setter for the lecturer attribute, so that it is writable;

      public void setLecturer(Staff lecturer) {

            this.lecturer = lecturer;

      }

      // 4)an enrolStudent method that adds a given Student object into the

      // 'enrolled' set;

      public void enrolStudent(Student s) {

            // simply adding

            enrolled.add(s);

      }

      // 5)a withdrawStudent method that removes a given Student object from the

      // 'enrolled' set and returns true if the student was successfully found and

      // removed, false otherwise.

      public boolean withdrawStudent(Student s) {

            // finding the student

            for (int i = 0; i < enrolled.size(); i++) {

                  if (enrolled.get(i).getId() == s.getId()) {

                        // found, removing and returning true

                        enrolled.remove(i);

                        return true;

                  }

            }

            return false; // not found

      }

}

Add a comment
Know the answer?
Add Answer to:
[Continued] Your Course class should have: 1)a getter for the course 'code', so that it is...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • [Continued] Your Course class should have: 1)a getter for the course 'code', so that it is...

    [Continued] Your Course class should have: 1)a getter for the course 'code', so that it is read-only; 2)a getter and setter for the lecturer attribute, so that it is readable and writable; 3)a constructor that creates the associations shown in the UML diagram; 4)an enrolStudent method that adds a given Student object into the 'enrolled' set; 5)a withdrawStudent method that removes a given Student object from the 'enrolled' set and returns true if the student was successfully found and removed,...

  • Extend the code from Lab6.A template is given to you with CircleHeader.h, Circle.cpp and CircleMain.cpp Use...

    Extend the code from Lab6.A template is given to you with CircleHeader.h, Circle.cpp and CircleMain.cpp Use the same Circle UML as below and make extensions as marked and as needed. Given below is a UML for the Painting Class. You will need to write PaintingHeader.h and Painting.cpp. The Painting Class UML contains a very basic skeleton. You will need to flesh out this UML and add more instance variables and methods as needed. You do NOT need to write a...

  • java language Problem 3: Shape (10 points) (Software Design) Create an abstract class that represents a Shape and contains abstract method: area and perimeter. Create concrete classes: Circle, Rectang...

    java language Problem 3: Shape (10 points) (Software Design) Create an abstract class that represents a Shape and contains abstract method: area and perimeter. Create concrete classes: Circle, Rectangle, Triangle which extend the Shape class and implements the abstract methods. A circle has a radius, a rectangle has width and height, and a triangle has three sides. Software Architecture: The Shape class is the abstract super class and must be instantiated by one of its concrete subclasses: Circle, Rectangle, or...

  • I Need UML Design for the following Java code: Colorable interface: public interface Colorable { //a...

    I Need UML Design for the following Java code: Colorable interface: public interface Colorable { //a void method named howToColor(). void howToColor(); } GeometricObject class: public class GeometricObject { } Sqaure class: public class Square extends GeometricObject implements Colorable{ //side variable of Square double side;    //Implementing howToColor() @Override public void howToColor() { System.out.println("Color all four sides."); }    //setter and getter methods for Square public void setSide(double side) { this.side = side; }    public double getSide() { return...

  • Java code for the following inheritance hierarchy figure.. 1. Create class Point, with two private instance...

    Java code for the following inheritance hierarchy figure.. 1. Create class Point, with two private instance variables x and y that represented for the coordinates for a point. Provide constructor for initialising two instance variables. Provide set and get methods for each instance variable, Provide toString method to return formatted string for a point coordinates. 2. Create class Circle, its inheritance from Point. Provide a integer private radius instance variable. Provide constructor to initialise the center coordinates and radius for...

  • Writing 3 Java Classes for Student registration /** * A class which maintains basic information about...

    Writing 3 Java Classes for Student registration /** * A class which maintains basic information about an academic course. */ public class Course {    /** * Attributes. */ private String code; private String title; private String dept; // name of department offering the course private int credits; /** * Constructor. */ public Course(String code, String title, int credits) { // TODO : initialize instance variables, use the static method defined in // Registrar to initialize the dept name variable...

  • Write a Python class, Circle, constructed by a radius and two methods which will compute the...

    Write a Python class, Circle, constructed by a radius and two methods which will compute the area and the perimeter of a circle. Include the constructor and other required methods to set and get class attributes. Create instances of Circle and test all the associated methods. Write a Python class, Rectangle, constructed by length and width and a method which will compute the area of a rectangle. Include the constructor and other required methods to set and get class attributes....

  • Given a class called Student and a class called Course that contains an ArrayList of Student....

    Given a class called Student and a class called Course that contains an ArrayList of Student. Write a method called getDeansList() as described below. Refer to Student.java below to learn what methods are available. I will paste both of the simple .JAVA codes below COURSE.JAVA import java.util.*; import java.io.*; /****************************************************** * A list of students in a course *****************************************************/ public class Course{     /** collection of Students */     private ArrayList<Student> roster; /***************************************************** Constructor for objects of class Course *****************************************************/...

  • Receiveing this error message when running the Experts code below please fix ----jGRASP exec: javac -g...

    Receiveing this error message when running the Experts code below please fix ----jGRASP exec: javac -g GeometricObject.java GeometricObject.java:92: error: class, interface, or enum expected import java.util.Comparator; ^ 1 error ----jGRASP wedge2: exit code for process is 1. ----jGRASP: operation complete. 20.21 Please code using Java IDE. Please DO NOT use Toolkit. You can use a class for GeometricObject. MY IDE does not have access to import ToolKit.Circle;import. ToolKit.GeometricObject;.import ToolKit.Rectangle. Can you code this without using the ToolKit? Please show an...

  • Assignment Requirements I have also attached a Class Diagram that describes the hierarchy of the inheritance...

    Assignment Requirements I have also attached a Class Diagram that describes the hierarchy of the inheritance and interface behaviors . The link to the PDF of the diagram is below MotorVehical.pdf Minimize File Preview User Define Object Assignment: Create a Intellij Project. The Intellij project will contain three user defined classes. The project will test two of the User Define Classes by using the invoking each of their methods and printing the results. You are required to create three UML...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT