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

  Recursive factorial program

Write a class Array that encapsulates an array and provides bounds-checked access. Create a recursive factorial program that prompts the user for an integer N and writes out a series of equations representing the calculation of N!.

  Hunt the wumpus game

Reprot on Hunt the Wumpus Game has Source Code listing, screen captures and UML design here and also, may include Javadoc source here.

  Create a gui interface

Create GUI Interface in java programing with these function: Sort by last name and print all employees info, Sort by job title and print all employees info, Sort by weekly salary and print all employees info, search by job title and print that emp..

  Plot pois on a graph

Write a JAVA program that would get the locations of all the POIs from the file and plot them on a map.

  Write a university grading system in java

University grading system maintains number of tables to store, retrieve and manipulate student marks. Write a JAVA program that would simulate a number of cars.

  Wolves and sheep: design a game

This project is designed a game in java. you choose whether you'd like to write a wolf or a sheep agent. Then, you are assigned to either a "sheep" or a "wolf" team.

  Build a graphical user interface for displaying the image

Build a graphical user interface for displaying the image groups (= cluster) in JMJRST. Design and implement using a Swing interface.

  Determine the day of the week for new year''s day

This assignment contains a java project. Project evaluates the day of the week for New Year's Day.

  Write a java windowed application

Write a Java windowed application to do online quiz on general knowledge and the application also displays the quiz result.

  Input pairs of natural numbers

Java program to input pairs of natural numbers.

  Create classes implement java interface

Interface that contains a generic type. Create two classes that implement this interface.

  Java class, array, link list , generic class

These 14 questions covers java class, Array, link list , generic class.

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