Question: HOMEWORK # 4 QUESTION # 1. Write a Java program that contains a method named DisplayStock. The input argument to this method is the number

HOMEWORK # 4 QUESTION # 1. Write a Java program that contains a method named DisplayStock. The input argument to this method is the number of days and the share points on the first day. The method is used to decrease and increase the share points in the same way as in Problem 1 of Homework #3. The method then displays a table showing the days and the share points on those days (as shown below). The method doesnt return anything.

The main method in the program will first ask the users to enter the number of days in the specified period and the share points on the first day (in the same way as Problem 1 in Homework #3. The program should then call the DisplayStock method that outputs the table (as shown below).

For example, if a user enters as below:

Number of days in the period: 11 Share points on the first day: 550

The program (using the DisplayStock method) outputs the following:

Day Share Points

1 550

2 600

3 650

4 700 5 750

6 800

7 775

8 750

9 725

10 700

11 675

(HOMEWORK # 3 PROBLEM # 1 REFERENCED IN THIS QUESTION)

Assume that the share points of a company fluctuate with a particular pattern in the New York Stock Exchange. It increases by 50 points every day in the first half of a given period and decreases by 25 points in the second half. If the number of days in a given period is odd, the first half is considered a day more than the second half.

Write a Java program that will first ask users to enter the number of days in a given period and the share points on the first day. The number of days that is entered must not be less than 10 and more than 20. If it is not so, the user must be prompted to enter the input again with the following message: The number of days doesnt meet the required criteria, enter it again. The program should output a table showing the days and the share points on those days.

For example, if a user enters as below:

Number of days in the period: 11 Share points on the first day: 550

The program should output the following:

Day Share Points

1 550

2 600

3 650

4 700

5 750

6 800

7 775

8 750

9 725

10 700

11 675

(CODE FOR HOMEWORK # 3 QUESTION 1)

//Import scanner for future use import java.util.Scanner; public class HW3_Q1 { public static void main(String[] args) { //Declare variables int points, day, totalDays; //Create Scanner class object to get inputs from the user Scanner sc = new Scanner(System.in); //While loop executes until the user enters a valid number while (true) { //Get input entered by the user and make sure if fits the criteria System.out.print("Number of days in the period: "); totalDays = sc.nextInt(); if (totalDays < 10 || totalDays >20) { //Lets the user know that the number they entered in not valid System.out.println("The number of days doesn't meet the required criteria, enter it again"); } else break; } //Getting the input from user for share points System.out.print("Share points on the first day: "); points = sc.nextInt(); //Display the output System.out.println("Day Share Points"); //Determines if the days are odd or even then executes code according to the user input if (totalDays % 2 != 0) { int i = 0; System.out.println(i + 1 + "\t" + points); for (i = 1; i < (totalDays / 2) + 1; i++) { points += 50; System.out.println(i + 1 + "\t" + points); } for (i = (totalDays / 2) + 1; i < totalDays; i++) { points -=25; System.out.println(i + 1 + "\t" + points); } } else { int i = 0; System.out.println(i + 1 + "\t" + points); for (i = 1; i < (totalDays / 2); i++) { points +=50; System.out.println(i + 1 + "\t" + points); } for (i = (totalDays / 2) + 1; i < totalDays; i++) { points -= 25; System.out.println(i + 1 + "\t" + points); } } //Display the output } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!