exam平台Java试题阶段(二)

说明:

以下为过程化考试平台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 
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
代理服务器是一种位于客户端和服务器之间的中间服务器,它可以拦截、过滤和转发客户端与服务器之间的请求和响应。代理服务器的作用有很多,主要包括以下几个方面。 首先,代理服务器可以提高网络请求的效率。代理服务器可以缓存请求的结果,当客户端再次发送相同的请求时,代理服务器可以直接返回已缓存的结果,减少了网络传输的开销。此外,代理服务器还可以进行压缩和合并请求,从而减少了网络传输的数据量,提高了网络请求的速度。 其次,代理服务器可以增强网络的安全性。代理服务器可以拦截客户端的请求,对请求进行检查和过滤,防止恶意程序和攻击进入服务器。代理服务器还可以对客户端和服务器间的通信进行加密,保护敏感信息的传输安全。 再次,代理服务器可以实现网络访问的控制和管理。通过代理服务器,网络管理员可以对客户端的访问进行限制和过滤,防止网络滥用和非法访问。代理服务器还可以根据客户端的IP地址、地理位置等进行访问控制,提供灵活的网络策略管理。 最后,代理服务器还可以实现负载均衡和内容筛选。代理服务器可以将客户端的请求分发到多台服务器上,实现负载均衡,提高服务器的性能和可用性。代理服务器还可以根据用户的需求进行内容筛选和修改,实现广告过滤、网页压缩等功能。 总之,代理服务器是一个非常重要的网络工具,它可以提高网络请求的效率和安全性,实现网络访问的控制和管理,以及负载均衡和内容筛选等功能。在Java中,我们可以使用Java提供的相关API和框架来实现代理服务器的功能和应用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值