Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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

ID: 3787378 • Letter: J

Question

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);
   }

}

Explanation / Answer

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()+" "+

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

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

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

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

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

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

   }

}

########## 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());

       }

   }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote