Define the rectangle2d class

Assignment Help JAVA Programming
Reference no: EM131591565

Exercise 1: Implement the Point class (code for this up to the isHigher method was discussed in the lecture). The class has the following instance variables:
- x coordinate (an int)
- y coordinate (an int) and the following methods:
- Constructor that sets the x and y coordinates
- Get and set methods

- Method equals if this point is equal to another point object (if the x and y coordinates are the same).
- Method isHigher if this point is higher than another point object (Note that we assume that the top left corner is the coordinate 0,0).

- Method findDistance that calculates the distance between this point and another point. (Formula for the distance between two points (x1,y1) and (x2,y2) is square root of ((x2-x1)2 + (y2-y1)2)

Test the class. In the demo program, construct four points p1, p2, p3 and p4 using user-defined values. Determine

- Which point is the highest. (If more than one point is at the same height, you may report any one point).
- Whether the distance p1 → p2 is more than the distance p3 → p4, or p3 → p4 is more than p1 →p2, or whether they are the same.

A sample screen dialog is given below:

Enter the x and y coordinates of point1: 8 9 Enter the x and y coordinates of point2: 4 3 Enter the x and y coordinates of point3: 2 1 Enter the x and y coordinates of point4: 5 6 [2,1] is the highest point
The distance between [8,9] and [4,3] is 7.211102550927978
The distance between [2,1] and [5,6] is 5.830951894845301
[8,9]-->[4,3] is longer than [2,1]-->[5,6]

Exercise 2: Implement a Stock class that has the following attributes: symbol (String), price (double), number of shares (int) and
the following methods:
Constructor (that sets the symbol, price and number of shares to user defined values). Get and set methods.
toString method returns the symbol, price and number of shares
method public int compareTo(Stock s) compares the share price of this stock with another stock and returns:
-1 if the value of this stock is lower than the other stock. 1 if the value of this stock is higher than the other stock. 0 if both the values are the same.
(Value of the stock = price * number of shares)

For example, let's say IBM has a price of $105.23 and you have bought 45 shares and MOS has a price of
$89.88 and you have bought 60 shares. Then the value of IBM in your portfolio is 105.23*45= $4735.35 and the value of MOS in your portfolio is 89.88*60 = $5392.80. Comparing this stock(IBM) with another stock(MOS) will return a -1 since the value of MOS is greater in your portfolio.

Test the Stock class with a client program that asks the user to enter the symbols, share prices, and number of shares for two stocks, prints their values, determines which stock is higher than the other and by how much and prints the total value. You must use the methods in the Stock class wherever appropriate.

A portion of the client program is given below for your programming convenience.
import java.util.Scanner; public class StockDemo
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in); String sym1, sym2;
double prc1, prc2;
int sh1, sh2;
//get the values for two stocks
System.out.print("Enter the symbols for the two stocks: "); sym1 = keyboard.next();
sym2 = keyboard.next();
System.out.print("Enter their prices:
"); prc1 = keyboard.nextDouble(); prc2 = keyboard.nextDouble();
System.out.print("Enter the number of shares for the two stocks: "); sh1 = keyboard.nextInt();
sh2 = keyboard.nextInt();

//create the first Stock
Stock s1 = new Stock(sym1,prc1,sh1);

//create the second Stock
Stock s2 = new Stock(sym2,prc2,sh2);

// continue the rest of the code here
}
}

The following is a sample screen dialog:
Enter the symbols for the two stocks: IBM MOS Enter their prices: 105.23 89.88
Enter the number of shares for the two stocks: 45 60

I have the following stocks: Stock: IBM
Price: 105.23
Shares: 45

Stock: MOS Price: 89.88
Shares: 60

The value of MOS is higher than IBM
The total value of my portfolio is: $ 10128.15

Exercise 3: Define the Rectangle2D class that contains:

- Double data fields named xpos, ypos (that specify the top left corner of the rectangle), width and height. (assume that the rectangle sides are parallel to the x and y axes. See example below).

77_Figure.jpg

- A no-arg constructor that creates a default rectangle with (0.0, 0.0) for (xpos, ypos) and 0.0 for both width and height.
- A constructor that creates a rectangle with the specified xpos, ypos, width, and height.
- Get and set methods for all the instance variables.
- A method getArea() that returns the area of the rectangle.
- A method getPerimeter() that returns the perimeter of the rectangle.
- A method contains(x, y) that returns true if the specified point (x, y) is inside this rectangle. See Figure 1(a).
- A method contains(Rectangle2D r) that returns true if the specified rectangle is inside this rectangle. See Figure 1(b). Note that the condition for contains is that all four corners of the second rectangle must be contained in the first. This means, that if the two rectangles are exactly identical, we still say that the second rectangle is contained within the other.

Write a test program that creates a Rectangle2D object and tests various methods. For example, as a first test case, create a Rectangle2D object r1 with parameters 2, 2, 5.5 and 4.9, which represent xpos, ypos, width and height, respectively, displays its area and perimeter, and displays the result of r1.contains(3, 3) and r1.contains(new Rectangle2D(4, 5, 10.5, 3.2)).

The sample screen dialog for the above input is given below:

Enter the xpos, ypos, width and height of the rectangle: 2 2 5.5 4.9 The perimeter of the rectangle is 20.8
The area of the rectangle is 26.950000000000003
Rectangle [[2.0,2.0],width=5.5,height=4.9] contains point [3,3] [[2.0,2.0],width=5.5,height=4.9] does not contain Rectangle

[[4.0,5.0],width=10.5,height=3.2]
Try it for two other cases. What to submit:
ONE Zip file containing the following:
1. Point.java
2. PointDemo.java
3. Stock.java
4. StockDemo.java
5. Rectangle2D.java
6. Rectangle2DDemo.java
7. A text file containing sample outputs for all the programs.

Reference no: EM131591565

Questions Cloud

Difference between good and bad table structures : What role does normalization play in good and bad table structures, and why is normalization so important to a good table structure
What would be tiny component cost of preferred stock : Tiny has preferred stock selling for 109.2 percent of par that pays an 9.0 percent annual coupon. What would be Tiny's component cost of preferred stock
Create a graphic organizer such as a table and timeline : Create graphic organizer such as table, timeline, Venn diagram, K-W-L chart, flow chart, or other visual tool to compare and contrast these philosophical views.
Assume all payments are paid at the end of the year : If the appropriate interest rate is 11 percent, what kind of deal did the player snag? Assume all payments are paid at the end of the year.
Define the rectangle2d class : Implement a Stock class that has the following attributes: symbol (String), price (double), number of shares - Define the Rectangle2D class
Define what are the health and illness beliefs and practices : What are the health and illness beliefs and practices that have been passed on through your family
Develop recommendations to help firms adopt technologies : Develop recommendations to help firms adopt technologies that support functional strategies aligned with their business strategy.
Plodding along without much attention from the stock market : Your firm has been plodding along without much attention from the stock market.
Various types of barriers to worldwide sourcing : The reading assignment describes various types of barriers to worldwide sourcing, as illustrated in Exhibit 10.4 of the text. Using the table that follows

Reviews

Write a Review

JAVA Programming Questions & Answers

  Create an application with a jframe and five labels

Create an application with a "JFrame" and five labels that contain the names of five friends. Every time the user clicks a "JButton", remove one of the labels and add a different one. Save the file ass JDisappearingFriends.java

  Write a program that reads in the file

Write a program that reads in the file and exports it to a standard CSV format. For the records above, the output format would be Freddy Kruger, 1313 Mockingbird Lane, Houston, Texas Billy Thornton, 1010 Slingblade Street, Houston, Texas

  Create new bluej project and a new class named cashregister

Create a new BlueJ project and a new class named CashRegister. Write a program that allows the user to enter the quantity of pennies

  Description of routes and services

Write a method for finding a journey from one station to another using an algorithm that is specified below in this handout.

  Vehicle in vehicle.java

Create a class called Vehicle in Vehicle.java with instance variables make (such as Ford, GM, Chrysler, Toyota, or Honda), year, and horsepower (200, 300, and 400). Add the necessary constructor and set and get methods.

  Cmpet 301 lab 11 overview

CMPET 301: Lab 11 Overview, Create a class for one playing card. Make a deck of playing cards by instantiating an array of 52 playing cards playing cards.

  Java class to accept a user-s hourly rate of pay

Write a class that accepts a user's hourly rate of pay and the number of hours worked. Display the user's gross pay, the withholding tax (15% of gross pay), and the net pay (gross pay - withholding).

  Write servlet code for accepting two numbers from user

Write the servlet code for accepting two numbers from user show addition and multiplication of numbers. If error occurs, then call JSP error page to display suitable error message.

  Implement a class called reversewords

Implement a class called ReverseWords that uses a stack to output a set of elements input by the user in reverse order.

  Program to convert temperature from fahrenheit to celsius

Write a Java program to convert temperature from Fahrenheit to Celsius - Change them to use a keyboard scanner instead of hard-coding values.

  What is one example from the list in table of popular

based on the keston 2013 article what are the advantages and disadvantages of using mashups? what is one example from

  Implement a method with three arguments

Implement a method with three arguments: a graph, a starting vertex number, and an ending vertex number. The method determines whether there is a directed path from the starting vertex to the ending vertex.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd