Ask Java Expert


Home >> Java

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.

Java, Programming

  • Category:- Java
  • Reference No.:- M92400278
  • Price:- $10

Priced at Now at $10, Verified Solution

Have any Question?


Related Questions in Java

Chatbotscreate a small networked chat application that is

Chatbots Create a small, networked chat application that is populated by bots. Introduction On an old server park, filled with applications from the early days of the internet, a few servers still run one of the earliest ...

Assignment taskwrite a java console application that allows

Assignment task Write a java console application that allows the user to read, validate, store, display, sort and search data such as flight departure city (String), flight number (integer), flight distance (integer), fl ...

Assignment game prototypeoverviewfor this assessment task

Assignment: Game Prototype Overview For this assessment task you are expected to construct a prototype level/area as a "proof of concept" for the game that you have designed in Assignment 1. The prototype should function ...

Assignment taskwrite a java console application that allows

Assignment task Write a java console application that allows the user to read, validate, store, display, sort and search data such as flight departure city (String), flight number (integer), flight distance (integer), fl ...

In relation to javaa what is constructor the purpose of

(In relation to Java) A. What is constructor? the purpose of default constructor? B. How do you get a copy of the object but not the reference of the object? C. What are static variables and instance variables? D. Compar ...

Project descriptionwrite a java program to traverse a

Project Description: Write a java program to traverse a directory structure (DirWalker.java) of csv files that contain csv files with customer info. A simple sample in provided in with the sample code but you MUST will r ...

Fundamentals of operating systems and java

Fundamentals of Operating Systems and Java Programming Purpose of the assessment (with ULO Mapping) This assignment assesses the following Unit Learning Outcomes; students should be able to demonstrate their achievements ...

Assessment -java program using array of Assessment -JAVA Program using array of objects

Assessment -JAVA Program using array of objects Objectives This assessment item relates to the course learning outcomes as stated in the Unit Profile. Details For this assignment, you are required to develop a Windowed G ...

Applied software engineering assignment 1 -learning

Applied Software Engineering Assignment 1 - Learning outcomes - 1. Understand the notion of software engineering and why it is important. 2. Analyse the risk factors associated with phases of the software development lif ...

Retail price calculatorwrite a java program that asks the

Retail Price Calculator Write a JAVA program that asks the user to enter an item's wholesale cost and its markup percentage. It should then display the item's retail price. For example: (If an item's wholesale cost is 5. ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As