[SOLVED] CS1301 Lab 09-Classes and Methods

35.00 $

Category:

Description

Rate this product

Good programmers think before they begin coding. Part I of this assignment involves brainstorming with a group of peers with no computers to talk about a strategy for solving this week’s lab. Breakup into groups based on how you’re seated (3-4 people per group) and brainstorm about how to solve the problems below. Make sure everyone understands the problem and sketch out potential ways to move toward a solution.  You may find it helpful to look over the required readings for this week.

 

Before you begin coding, you should be able to answer the following questions:

  • Why is it good practice to make our instance variables private?
  • What would happen if you used == to compare two objects?
  • When writing an equals method, you should be comparing two objects – the one that called the method and the one that was passed in as the actual parameter. Inside the equals method definition, how do you access the attributes of each of these objects? You may want to use the example in the textbook for guidance.
  • How do we compare doubles? (don’t use ==)

 

Note: Brainstorms are to help you get started with the lab and get you in the habit of designing before you code.  You won’t submit them to eLC.

Introduction

This lab introduces you to fundamental concepts of Object Oriented Programming (OOP), arguably the dominant programming paradigm in use today. In the paradigm, a program consists of component parts (objects) that are independent of each other and that interact in order to achieve a desired result. At a very basic level, an object-oriented program consists of the following types of things.

Classes:  The template or blueprint from which objects are created.

Objects:  The instantiation of a class. Once a class (the template) has been defined, many instances of it can be created.

Methods:  The named procedures or functions defined for an object. A method accepts input arguments and contains statements that are executed when the method is invoked. Some methods return values.

Instance 

Variables: Variables that are initialized when an object is created. Each instance of a class can have distinct values for these variables.

 

In this lab, you will practice working with these fundamentals. A skeleton of code has been written for you defining the basic class and method structure that you are to use. You will then add code to the existing skeleton methods so that the classes function as intended. You are then to use a testing program illustrate how the component parts of what you have created interact.

Lab Objectives

By the end of the lab, you should be able to create classes utilizing:

  • access modifiers;
  • instance variables (also called fields);
  • methods which return values and void methods (which do not return any value);
  • methods calling other methods;
  • accessor and mutator methods; The equals() method.

What to Submit

The files Circle.java and CircleTester.javashould be submitted to eLC for grading.

 

Instructions

 

The file Circle.java contains a partial definition for a class representing a circle. Save it to your computer and study it to see what methods it contains. Then complete the Circle class as described below. Note that you won’t be able to test your methods until you complete the test client class called CircleTester.java.

 

  1. You must include a comment stating your name, the date, program purpose, and containing a statement of academic honesty. When writing, you must also abide by the Java formatting and naming conventions.
  2. Complete the Circle class following the instructions detailed below:
    1. Declare four private instance variables: name, x, y and radius. The instance variables x and y represent the coordinates of the center of the circle. Note that you should not declare any other instance variables other than these.
    2. Fill in the code for method toString, which should return a “string” containing values for the name of the circle, the center of the circle, x and y, and the radius of the circle. Note:

this method does not print the string – it returns the string.  The returned string value should be formatted as:

name: name center: (x,y) radius: r

  1. Fill in the code for the accessor (getter) methods: getName, getX, getY and getRadius Note that accessors are also called “getter” methods.
  2. Fill in the code for the mutator (setter) methods: setName, setX and setY properly. Mutators are also called “setter” methods.
  3. Fill in the code for the mutator(setter) method: setRadius Note that:
    1. If the new radius value passed as a parameter to the method is not valid, than the method should leave the original radius unchanged.
    2. A radius is valid only when it is greater than or equal to 0.
  4. Fill in the code for the method area that returns the area of the circle, computed by

PI * radius2 //Use the value of PI provided by the constant Math.PI.

  1. Fill in the code of the method perimeter that returns the perimeter of the circle computed by

 2 * PI * radius

  1. Fill in the code of the method diameter that returns the diameter of the circle.
  2. Fill in the code of the method isUnitCircle that returns true if the radius of the circle is 1 and is centered at the origin, i.e., the coordinates of the circle’s center are (0, 0). Otherwise, this method returns false. Note: since you’re comparing doubles, you should not use ==.  Remember how to properly compare doubles from lecture.
  3. The CircleTester.java file contains a driver program to try and test out your Circle

Using the comments in the CircleTester.java file, finish the file so that it does the following:

  1. Display the name, center and radius of circle1.
  2. Set the radius of circle2 to 3.
  3. Display the name, center and radius of circle2.
  4. Display the diameter, area and perimeter of circle1.
  5. Display the diameter, area and perimeter of circle2.
  6. Display a message that indicates whether or not circle1 is a unit circle.
  7. Display a message that indicates whether or not circle2 is a unit circle.
  1. Compile and test your Circle and CircleTester files, and verify your code is working properly. Then, set breakpoints in your methods, and the first line of the CircleTester class, and observe the flow of control within an Object-Oriented program.
  2. Add to your Circle class the following methods:

a. public boolean equals(Circle anotherCircle)

This method returns true when the radius and centers of both circles are the same; otherwise, it returns false. This method can be implemented in one line.  Remember not to compare doubles using ==.

 

b. public double distance(Circle anotherCircle)

This method returns the distance between the centers of the circle executing the method and anotherCircle. Let (x, y) and (xa, ya) be the centers of the circle executing the method and anotherCircle respectively, the distance between their centers is computed by

(𝑥𝑥−𝑥𝑥𝑎𝑎)2+(𝑦𝑦−𝑦𝑦𝑎𝑎)2

The Math class contains methods for computing both square roots and powers.  Below are the definitions of the methods.

//Returns a double value containing the square root of a double number. double sqrt(double number) 

 

//Returns a double value of the first argument raised to the power of the second argument.

double pow(double base, double exponent)

 

c. public boolean isSmaller(Circle anotherCircle)

 The method isSmaller returns true when the circle executing the method (calling object) is  smaller than the parameter (anotherCircle).  Otherwise, it returns false.  To see which is larger,  use the diameter of each circle.  You can use the diameter method written earlier to get this  value from each object.  Do this instead of using radius to practice calling methods within  methods.

d. public int compareTo(Circle anotherCircle)

Works similar to the compareTo method in the String class. If the calling object is larger, the  method returns a positive 1.  If the calling object is smaller than anotherCircle, it returns -1.   Otherwise, it returns 0.  You may want to use the isSmaller method in your implementation.

e. public boolean intersects(Circle anotherCircle)

The method intersects returns true when the circle executing the method and anotherCircle have an intersecting area (one or more points enclosed by both circles); otherwise, it returns false.

Two circles intersect (by this lab’s definition) if the distance between the centers of the two circles is less than the sum of their radius. Your method must call the method distance to obtain the distance between the two circles.

  1. After you’ve added these methods, add at least three different tests for each method to your CircleTester class, and verify you have successfully written the above methods. Remember, your methods must work properly with any input we can provide, so test rigorously. For each test, you should output pass/fail instead of dumping values to the screen.  An example can be found in the test file.
  2. Check your method signatures using CircleMethodCheck.java (found on the course website). If you pass the tests, it means you are using the proper method signatures.  Passing these tests does not guarantee that you have implemented the methods correctly but it helps avoid you missing points due to invalid headers (signatures).  To use this class, simply put it into the src folder of your project and then run CircleMethodCheck.java.