Question

In: Computer Science

Java program Create two classes based on the java code below. One class for the main...

Java program

Create two classes based on the java code below. One class for the main method (named InvestmentTest) and the other is an Investment class. The InvestmentTest class has a main method and the Investment class consists of the necessary methods and fields for each investment as described below.

1.The Investment class has the following members:

a. At least six private fields (instance variables) to store an Investment name, number of shares, buying price, selling price, and buying commission and sales commission.

b. Write set and get methods to access each of the private fields

c. “InvestmentSummary” method to print Investment Information including the result of

3. Create two different objects of the investment class

4. Create an object of java.util.Scanner to input the information about each investment, one at a time form the keyboard, and track the information in the proper Investment objects by placing calls to the proper set methods for each of the two objects.

5. When invoking the InvestmentSummary method, use the get method found in the Investment class to access the private members.

    a. Print the investment information including gain or loss using the string %s specifier.

StockProfit.java

import java.text.DecimalFormat;
import java.util.Scanner;

public class StockProfit {

   public static void main(String[] args) {
  
       DecimalFormat df=new DecimalFormat("#.##");
       //Scanner class object is used to read the inouts entered by the user
       Scanner kb=new Scanner(System.in);
      
       int ns; // Number of shares
double sp; // Sale price per share
double sc; // Sale commission
double pp; // Purchase price per share
double pc; // Purchase commission
double prof; // Profit from a sale
  
// Get the number of shares.
System.out.print("How many shares did you buy and then sell? ");
ns=kb.nextInt();
  
// Get the purchase price per share.
System.out.print("What price did you pay for the stock per share? ");
pp=kb.nextDouble();

// Get the purchase commission.
System.out.print("What was the purchase commission? ");
pc=kb.nextDouble();

// Get the sale price per share.
System.out.print("What was the sale price per share? ");
sp=kb.nextDouble();

// Get the sales commission.
System.out.print("What was the sales commission? ");
sc=kb.nextDouble();

// Get the profit or loss.
prof = profit(ns, pp, pc, sp, sc);

// Display the result.
System.out.print("The profit from this sale of stock is $"+df.format(prof));

   }
   // ********************************************************
   // The profit function accepts as arguments the number of *
   // shares, the purchase price per share, the purchase *
   // commission paid, the sale price per share, and the *
   // sale commission paid. The function returns the profit *
   // (or loss) from the sale of stock as a double. *
   // ********************************************************
   private static double profit(int ns, double pp, double pc, double sp,
           double sc) {
       return ((ns * sp) - sc) - ((ns * pp) + pc);
   }

}

Solutions

Expert Solution

Hi, Please find my implementation.

Please let me know in case of any issue.

########## Investment.java #########################

public class Investment {

   private String name;

   private int nu_of_shares;

   private double buy_price;

   private double sell_price;

   private double buy_commision;

   private double sell_commision;

   // getters and setters

   public String getName() {

       return name;

   }

   public int getNu_of_shares() {

       return nu_of_shares;

   }

   public double getBuy_price() {

       return buy_price;

   }

   public double getSell_price() {

       return sell_price;

   }

   public double getBuy_commision() {

       return buy_commision;

   }

   public double getSell_commision() {

       return sell_commision;

   }

   public void setName(String name) {

       this.name = name;

   }

   public void setNu_of_shares(int nu_of_shares) {

       this.nu_of_shares = nu_of_shares;

   }

   public void setBuy_price(double buy_price) {

       this.buy_price = buy_price;

   }

   public void setSell_price(double sell_price) {

       this.sell_price = sell_price;

   }

   public void setBuy_commision(double buy_commision) {

       this.buy_commision = buy_commision;

   }

   public void setSell_commision(double sell_commision) {

       this.sell_commision = sell_commision;

   }

   @Override

   public String toString() {

       // TODO Auto-generated method stub

       return super.toString();

   }

   /*********************************************************/

   // The profit function accepts as arguments the number of *

   // shares, the purchase price per share, the purchase *

   // commission paid, the sale price per share, and the *

   // sale commission paid. The function returns the profit *

   // (or loss) from the sale of stock as a double. *

   // ********************************************************

   private static double profit(int ns, double pp, double pc, double sp, double sc) {

       return ((ns * sp) - sc) - ((ns * pp) + pc);

   }

  

   public String InvestmentSummary(){

       return "Name: "+getName()+"\n"+

               "Buying price per share: "+getBuy_price()+"\n"+

               "Selling price per share: "+getSell_price()+"\n"+

               "Buying commision: "+getBuy_commision()+"\n"+

               "Selling commision: "+getSell_commision()+"\n"+

               "Number of shares: "+getNu_of_shares()+"\n"+

               "Profit/Looss: "+profit(nu_of_shares, buy_price, buy_commision, sell_price, sell_commision)+"\n";

   }

}

########## InvestmentTest.java #########################

import java.text.DecimalFormat;

import java.util.Scanner;

public class InvestmentTest {

   public static void main(String[] args) {

       DecimalFormat df=new DecimalFormat("#.##");

       //Scanner class object is used to read the inouts entered by the user

       Scanner kb=new Scanner(System.in);

       String name; // name

       int ns; // Number of shares

       double sp; // Sale price per share

       double sc; // Sale commission

       double pp; // Purchase price per share

       double pc; // Purchase commission

       double prof; // Profit from a sale

       int i = 0;

       // taking input for two investement details

       while(i < 2){

           // Get the name

           System.out.print("Enter name: ");

           name = kb.nextLine();

           // Get the number of shares.

           System.out.print("How many shares did you buy and then sell? ");

           ns=kb.nextInt();

           // Get the purchase price per share.

           System.out.print("What price did you pay for the stock per share? ");

           pp=kb.nextDouble();

           // Get the purchase commission.

           System.out.print("What was the purchase commission? ");

           pc=kb.nextDouble();

           // Get the sale price per share.

           System.out.print("What was the sale price per share? ");

           sp=kb.nextDouble();

           // Get the sales commission.

           System.out.print("What was the sales commission? ");

           sc=kb.nextDouble();

          

           // creating Investment Object

           Investment invest = new Investment();

           invest.setName(name);

           invest.setNu_of_shares(ns);

           invest.setBuy_price(pp);

           invest.setBuy_commision(pc);

           invest.setSell_price(sp);

           invest.setSell_commision(sc);

          

           // printing investment summary

           System.out.println(invest.InvestmentSummary());

       }

   }

}


Related Solutions

Create a java program that has a code file with main() in it and another code...
Create a java program that has a code file with main() in it and another code file with a separate class. You will be creating objects of the class in the running program, just as the chapter example creates objects of the Account class. Your system handles employee records and processes payroll for them. Create a class called Employee that holds the following information: first name, last name, monthly salary, and sales bonus. The class should have all the gets...
Create a Java program. The class name for the program should be 'EncryptText'. In the main...
Create a Java program. The class name for the program should be 'EncryptText'. In the main method you should perform the following: You should read a string from the keyboard using the Scanner class object. You should then encrypt the text by reading each character from the string and adding 1 to the character resulting in a shift of the letter entered. You should output the string entered and the resulting encrypted string. Pseudo flowchart for additional code to be...
Java Class Create a class with a main method. Write code including a loop that will...
Java Class Create a class with a main method. Write code including a loop that will display the first n positive odd integers and compute and display their sum. Read the value for n from the user and display the result to the screen.
JAVA A simple Class in a file called Account.java is given below. Create two Derived Classes...
JAVA A simple Class in a file called Account.java is given below. Create two Derived Classes Savings and Checking within their respective .java files. (modify display() as needed ) 1. Add New private String name (customer name) for both, add a New int taxID for Savings only. 2. Add equals() METHOD TO CHECK any 2 accounts Demonstrate with AccountDemo.java in which you do the following: 3. Create 1 Savings Account and 3 Checking Accounts, where 2 checkings are the same....
Java Programming Create a class named Problem1, and create a main method, the program does the...
Java Programming Create a class named Problem1, and create a main method, the program does the following: - Prompt the user to enter a String named str. - Prompt the user to enter a character named ch. - The program finds the index of the first occurrence of the character ch in str and print it in the format shown below. - If the character ch is found in more than one index in the String str, the program prints...
Classes and Objects Write a program that will create two classes; Services and Supplies. Class Services...
Classes and Objects Write a program that will create two classes; Services and Supplies. Class Services should have two private attributes numberOfHours and ratePerHour of type float. Class Supplies should also have two private attributes numberOfItems and pricePerItem of type float. For each class, provide its getter and setter functions, and a constructor that will take the two of its private attributes. Create method calculateSales() for each class that will calculate the cost accrued. For example, the cost accrued for...
Task 2/2: Java program Based upon the following code: public class Main {   public static void...
Task 2/2: Java program Based upon the following code: public class Main {   public static void main( String[] args ) {     String alphabet = "ABCDEFGHIJKLMNMLKJIHGFEDCBA";     for( <TODO> ; <TODO> ; <TODO> ) {       <TODO>;     } // Closing for loop   } // Closing main() } // Closing class main() Write an appropriate loop definition and in-loop behavior to determine if the alphabet string is a palindrome or not. A palindrome is defined as a string (or more generally, a token) which...
In Java Create a class called "TestZoo" that holds your main method. Write the code in...
In Java Create a class called "TestZoo" that holds your main method. Write the code in main to create a number of instances of the objects. Create a number of animals and assign a cage and a diet to each. Use a string to specify the diet. Create a zoo object and populate it with your animals. Declare the Animal object in zoo as Animal[] animal = new Animal[3] and add the animals into this array. Note that this zoo...
I need this Java code transform to Python Code PROGRAM: import java.util.Scanner; public class Main {...
I need this Java code transform to Python Code PROGRAM: import java.util.Scanner; public class Main { static int count=0; int calculate(int row, int column) { count++; if(row==1&&column==1) { return 0; } else if(column==1) { return ((200+calculate(row-1,column))/2); } else if(column==row) { return (200+calculate(row-1,column-1))/2; } else { return ((200+calculate(row-1,column-1))/2)+((200+calculate(row-1,column))/2); }    } public static void main(String[] args) { int row,column,weight; Main m=new Main(); System.out.println("Welcome to the Human pyramid. Select a row column combination and i will tell you how much weight the...
Writing Classes I Write a Java program containing two classes: Dog and a driver class Kennel....
Writing Classes I Write a Java program containing two classes: Dog and a driver class Kennel. A dog consists of the following information: • An integer age. • A string name. If the given name contains non-alphabetic characters, initialize to Wolfy. • A string bark representing the vocalization the dog makes when they ‘speak’. • A boolean representing hair length; true indicates short hair. • A float weight representing the dog’s weight (in pounds). • An enumeration representing the type...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT