Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
CSC 2010 Lab 6 
Problem 1 ( P 81, Q 4) 
• Write a program named SellStocks that calculates the value of a stock sale. The user will be 
prompted to enter the stock price, the number of shares to be sold, and the commission rate. The 
program will calculate the value of the shares by multiplying the stock price by the number of 
shares. It will also calculate the commission (the value of the shares multiplied by the commission 
rate) and the net proceeds(the value of the shares minus the commission). The following example 
shows what the user will see on the screen: 
 
This program calculates the net proceeds from a sale of stock. 
 
Enter a stock price: 10.125 
Enter number of shares: 11 
Value of shares: $111.38 
 
Enter commission rate (as a percentage): 1.5 
Commission: $1.67 
Net proceeds: $109.71 
 
• Note that the stock price, number of shares, and commission may contain digits after the decimal 
point. Dollar amount must be rounded to the nearest cent. 
Solution 
• import java.text.DecimalFormat; 
• import java.util.Scanner; 
 
• public class SellStocks { 
 
•  public static void main(String[] args){ 
•    
•   System.out.println("This program calculates the net proceeds from a sale of 
stock."); 
•   Scanner readIn = new Scanner(System.in); 
•   double stockPrice = 0; 
•   double numberShares = 0; 
•    
•   System.out.print("Enter a stock price: "); 
•   stockPrice = readIn.nextDouble(); 
•    
•   System.out.print("Enter number of shares: "); 
•   numberShares = readIn.nextDouble(); 
Solution cond. 
•    
•   DecimalFormat df = new DecimalFormat("0.##"); 
•   double valueShares = 
Double.parseDouble(df.format(stockPrice*numberShares)); 
•    
•   System.out.println("Value of shares: $"+valueShares); 
•    
•   System.out.print("Enter commission rate (as a percentage): "); 
•   double commissionRate = readIn.nextDouble(); 
•    
•   double commission = 
Double.parseDouble(df.format(commissionRate/100*valueShares)); 
•    
•   System.out.println("Commission: $"+commission); 
•   System.out.println("Net proceeds: $"+df.format(valueShares - commission)); 
•  } 
•   
• }