Project 6, COSC 111 Due Date: Oct. 29, 2015 Class Point The goal of this project
ID: 3759344 • Letter: P
Question
Project 6, COSC 111 Due Date: Oct. 29, 2015
Class Point
The goal of this project is to learn how to define a new data type (a class type) and to write a program that uses that data type. Create a BlueJ project (lab6), and then create two classes named ‘Point’ and ‘TestPoint’. Good programming style is important and points will be taken off for sloppy programs (see pages 46-51 of your textbook).
Write a class called ‘Point’ that represents a point in the xy-plane. The class includes two instance variables (data members) and supports three actions (has three methods):
a- Declare two private instance variables of type double called x and y.
b- Write a void method called ‘setData’ that takes two inputs of type double (has two parameters of type double). The purpose of this method is to assign a value to each of the instance variables. It assigns the value of the first parameter to the instance variable x, and the value of the second parameter to the instance variable y.
c- Write a method called ‘quadrant’ that takes no input (i.e., has no parameters) and returns a value of type int. This method will determine to which of four quadrants the calling object belongs (the calling object is an object that will invoke this method). It returns 1 if the calling object is in the first quadrant (x>=0, y>=0), 2 if the calling object is in the second quadrant (x<0, y>=0), 3 if the calling object is in the third quadrant (x<0, y<0), and 4 if it is in the fourth quadrant (x>=0, y<0).
d- Write a void method called ‘print’ that displays the value of the instance variables x and y as a pair on the screen (for example, if x is 2 and y is 5, it displays them as a pair
(2 , 5) ). Note that the values of x and y are printed as whole numbers and not decimal numbers.
**************** switch partner’s roles at this point *****************
Write a test program (a driver program) inside the class ‘TestPoint’ that does the following:
1- create an object of type Point named ‘first’.
2- prompt for and read in two decimal numbers. These numbers are the x and y coordinates of a point in the xy-plane.
3- call the method ‘setData’ to initialize the instance variables of object ‘first’ with the coordinates read in part 2.
4- call the method ‘print’ in order to display the object ‘first’ as shown in the sample output below (e.g., System.out.print(“first = “); first.print();).
5- call the method ‘quadrant’ to find out to which quadrant the object ‘first’ belongs, and use that information (the value returned by the method quadrant) to determine what message to print out (see sample output below). This is a good place for a switch statement! The expression that controls the switch statement will be an invocation of the method quadrant (e.g., switch (first.quadrant()) {…}).
After getting this program to work, modify it so that it allows the user to repeat the above process as many times as she/he wishes (use a do-while loop).
Sample input/output: The output of your program must exactly match the format shown below.
Programmer: Name of the programmer(s)
Course: COSC 111, Fall 2015
Lab#: 6
Due date: 10-27-2015
Enter the x and y coordinates of a point: 1 3
first = (1 , 3)
(1 , 3) is in the first quadrant
Do it again, yes (or no)? Yes
Enter the x and y coordinates of a point: 2 -5
first = (2 , -5)
(2 , -5) is in the fourth quadrant
Do it again, yes (or no)? yES
Enter the x and y coordinates of a point: -3 7
first = (-3 , 7)
(-3 , 7) is in the second quadrant
Do it again, yes (or no)? yes
Enter the x and y coordinates of a point: -1 -2
first = (-1 , -2)
(-1 , -2) is in the third quadrant
Do it again, yes (or no)? no
Explanation / Answer
import java.util.*;
class Point
{
private
double x,y;
public
void setData(double x1, double y1)
{
x=x1;
y=y1;
}
int quadrant()
{
if(x>=0 && y>=0)
return 1;
if(x<0 && y>=0)
return 2;
if(x<0 && y<0)
return 3;
if(x>=0 && y<0)
return 4;
return 0;
}
void print()
{
System.out.println("("+x+", "+y+")");
}
}
class TestPoint
{
public static void main(String args[])
{
Point first=new Point();
String ans;
int q;
Scanner o=new Scanner(System.in);
do
{
System.out.println("Enter 2 Decimal
numbers");
double xx = Double.parseDouble
(o.nextLine());
double yy = Double.parseDouble
(o.nextLine());
first.setData(xx,yy);
System.out.print("first = ");
first.print();
q=first.quadrant();
switch(q)
{
case 1:
System.out.println
("("+xx+", "+yy+")"+"is in first quadrant");
break;
case 2:
System.out.println
("("+xx+", "+yy+")"+"is in second quadrant");
break;
case 3:
System.out.println
("("+xx+", "+yy+")"+"is in third quadrant");
break;
case 4:
System.out.println
("("+xx+", "+yy+")"+"is in fourth quadrant");
break;
}
System.out.println("Do it again, yes (or
no)?");
ans=o.nextLine();
q=0;
}while (ans.equals("yes"));
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.