Make the following changes to the SellStocks program: (a) Add a loop so that, af
ID: 3630044 • Letter: M
Question
Make the following changes to the SellStocks program:(a) Add a loop so that, after displaying the net proceeds, the program will ask the user for another stock price, number of shares, and commission rate. The process will repeat indefinitely until the user enters zero as the stock price.
(b) Have the program put two digits after the decimal point when displaying dollar amounts. (The original program would display $10.5 instead of $10.50.)
Here is the source code:
// Please include single line comments with explanation next to where you made //changes
import java.util.*;
public class SellStocks {
public static void main (String [] args) {
Scanner in=new Scanner (System.in);
double price,rate,value,comm,net;
int shares;
System.out.print ("Enter stock price:");
price=in.nextDouble ();
System.out.print ("Enter number of shares:");
shares=in.nextInt ();
value=price*shares;
value=Math.round (value*100);
value/=100;
System.out.println ("Value of shares: $"+value);
System.out.print ("Enter commission rate (as a percentage): ");
rate=in.nextDouble ();
comm=value*(rate/100.);
comm=Math.round (comm*100);
comm/=100;
System.out.println ("Commission: $"+comm);
net=value-comm;
net=Math.round (net*100);
net/=100;
System.out.println ("Net proceeds: $"+net);
}
}
Explanation / Answer
please rate - thanks
import java.util.*;
public class SellStocks {
public static void main (String [] args) {
Scanner in=new Scanner (System.in);
double price,rate,value,comm,net;
int shares;
System.out.print ("Enter stock price(0 to exit):"); //add to prompt
price=in.nextDouble ();
while(price!=0) //keep going?
{System.out.print ("Enter number of shares:");
shares=in.nextInt ();
value=price*shares;
value=Math.round (value*100);
value/=100;
System.out.println ("Value of shares: $"+value);
System.out.print ("Enter commission rate (as a percentage): ");
rate=in.nextDouble ();
comm=value*(rate/100.);
comm=Math.round (comm*100);
comm/=100;
System.out.printf ("Commission: $%.2f ",comm); //use printf for 2 decimal
net=value-comm;
net=Math.round (net*100);
net/=100;
System.out.printf("Net proceeds: $%.2f ",net); //use printf for 2 decimal
System.out.print ("Enter stock price(0 to exit):"); //add to prompt
price=in.nextDouble (); //get next price
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.