说明:
以下为过程化考试平台Java练习题,代码是自己写的,由于我的水平很有限,不免有不妥之处,欢迎指正赐教,谢谢!
1.en_ 2017_ sw_ p2_001 Define a stock class
此题代码能够在eclipse中运行,但在exam编译器上编译结果为0分,请大神赐教!
Design a class named Stock that contains:
■ A private string data field named symbol for the stock’s symbol.
■ A private double data field named previousClosingPrice that stores the stock price for the previous day.
■ A private double data field named currentPrice that stores the stock price for the current time.
■ A constructor that creates a stock with the specified symbol.(The first constructor)
■ A constructor that creates a stock with the specified symbol, previousClosingPrice and currentPrice . This constructor must use this to invoke the first constructor to initialize the symbol.
■ Setters and getters for previousClosingPrice and currentPrice.
■A method named getChangePercent() that returns the percentage changed from previousClosingPrice to currentPrice.
■A method named printStockInfo() that print the stock info including symbol, previousClosingPrice , currentPrice and changed percent. The change percent retains 2 decimal places and end with % sign.The print example is :
■ Since the Main class can not be modified so you define the Stock class must base on the invoking method code in the main method.
SOHU 100 90
Stock:SOHU
Previous Closing Price:100.0
Current Price:90.0
Price Change:-10.00%
■ Complete the code in the main method according to the comments.
Output example:
Enter the stock’s symbol, previousClosisngPrice and currentPrice:
SKY 34.5 36.23
Stock:SKY
Previous Closing Price:34.5
Current Price:36.23
Price Change:5.01%
Stock:SINA
Previous Closing Price:89.5
Current Price:98.4
Price Change:9.94%
import java.util.Scanner;
/******start******/
class Stock {
private String symbol;
private double previousClosingPrice;
private double currentPrice;
public Stock(String newSymbol)
{
this.symbol = newSymbol;
}
public void setSymbol(String symbol){
this.symbol= symbol;
}
public String getSymbol(){
return symbol;
}
public double getChangePercent()
{
return (currentPrice - previousClosingPrice)/previousClosingPrice;
}
public double getPreviousClosingPrice()
{
return previousClosingPrice;
}
public void setPreviousClosingPrice(double previous)
{
this.previousClosingPrice = previous;
}
public double getCurrentPrice()
{
return currentPrice;
}
public void setCurrentPrice(double current)
{
this.currentPrice = current;
}
public void printStockInfo(){
System.out.println("Stock:" + getSymbol());
System.out.println("Previous Closing Price:" + getPreviousClosingPrice());
System.out.println("Current Price:" + getCurrentPrice());
System.out.println("Price Change:" + String.format("%.2f", getChangePercent() * 100) + "%");
}
}
/******end******/
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the stock's symbol, previousClosisngPrice and currentPrice:");
String symbol = scanner.next();
double previousClosingPrice = scanner.nextDouble();
double currentPrice = scanner.nextDouble();
/******start******/
//create a stock instance using the user enter data and invoke printStockInfo method to print stock info.
Stock stock1 = new Stock("SKY");
stock1.setPreviousClosingPrice(34.5);
stock1.setCurrentPrice(36.23);
stock1.printStockInfo();
/******end******/
Stock stock2 = new Stock("SINA");
stock2.setPreviousClosingPrice(89.5);
stock2.setCurrentPrice(98.4);
stock2.printStockInfo();
}
}
2.en_ 2017_ sw_ p2_002 Design MyInteger class
Design a class named MyInteger. The class contains:
■ An private int data field named value that stores the int value represented by this object.
■ A constructor MyInteger(int) that creates a MyInteger object for the specified int value.
■ A getter method that returns the int value.
■ The methods isEven() and isOdd() that return true if the value in this object is even or odd respectively.
■ The static methods isEven(int) and isOdd(int) that return true if the specified value is even or odd respectively.
■ The static methods isEven(MyInteger) and isOdd(MyInteger) that return true if the specified value is even or odd, respectively.
■ The methods equals(MyInteger) that return true if the value in this object is equal to the specified value.
■ A static method parseInt(String) that converts a string into an int value.
■ Since the Main class can not be modified so you define the MyInteger class must base on the invoking method code in the main method.
Output example:
Enter a int number to create MyInteger object:
35 901
Enter a string which can transform to a int:
myInteger1 is even? false
myInteger1 is odd? true
myInteger2 is even? false
myInteger1 is equal to myInteger2? false
5 is even? false
9 is odd? true
Enter a int number to create MyInteger object:
55 55
Enter a string which can transform to a int:
myInteger1 is even? false
myInteger1 is odd? true
myInteger2 is even? false
myInteger1 is equal to myInteger2? true
5 is even? false
9 is odd? true
import java.util.Scanner;
/******start******/
class MyInteger{
private int value;
public MyInteger(int value){
this.value=value;
}
public int getter(){
return value;
}
public boolean isEven(){
return isEven(this.value);
}
boolean isOdd(){
return isOdd(this.value);
}
static public boolean isEven(int value){
return value%2==0;
}
static public boolean isOdd(int value){
return value%2!=0;
}
static public boolean isEven(MyInteger value){
return value.isEven();
}
static public boolean