1. 程式人生 > >exam平臺Java試題階段(二)

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 isOdd(MyInteger value){
	return value.isOdd();
}
public boolean equals(int value){
    return this.value==value;
}
public boolean equals(MyInteger value){
    return value.equals(this.value);
}
public static int parseInt(char[] cs){
    String s=new String(cs);
    return parseInt(s);
}
 
public static int parseInt(String s){
    return Integer.valueOf(s);
}

}

/******end******/
public class Main {
public static void main(String[] args) {
	Scanner scanner = new Scanner(System.in);
	System.out.println("Enter a int number to create MyInteger object:");
	int value = scanner.nextInt();
	System.out.println("Enter a string which can transform to a int:");
	String str = scanner.next();
	MyInteger myInteger1 = new MyInteger(value);
	System.out.println("myInteger1 is even? " + myInteger1.isEven());
    System.out.println("myInteger1 is odd? " + myInteger1.isOdd());
    MyInteger myInteger2 = new MyInteger(MyInteger.parseInt(str));
    System.out.println("myInteger2 is even? " + MyInteger.isEven(myInteger2) );
    System.out.println("myInteger1 is equal to myInteger2? "+myInteger1.equals(myInteger2));
    System.out.println("5 is even? " + MyInteger.isEven(5));
    System.out.println("9 is odd? " + MyInteger.isOdd(9));
    scanner.close();
}
}

3.en_ 2017_ sw_ p2_003 Design MyPoint class

Design a class named MyPoint to represent a point with x-coordinates and y-coordinates. The class contains:

■ The private data fields(double) x and y that represent the coordinates with get methods.

■ A no-arg constructor that creates a point (0, 0).

■ A constructor that constructs a point with specified coordinates.

■ Two get methods for the data fields x and y, respectively.

■ A method named distance(MyPoint anotherPoint) that returns the distance from this point to another point of the MyPoint type.

■ A method named distance(double x,double y) that returns the distance from this point to another point with specified x- and y-coordinates.

■ A static method named distance(MyPoint p1,MyPoint p2) that returns the distance from the p1 point to another point p2.

■ Since the Main class can not be modified so you define the MyPoint class must base on the invoking method code in the main method.

Tips: The coordinates of the two points are (x1,y1) and (x2,y2) then the formula for calculating the distance between two points is

d=√[(x1-x2)²+(y1-y2)²]

output example:

Enter x-coordinates and y-coordinates:

13.5 22

The distance between p1 and p2 is:28.443101096751036

The distance between p2 and p3 is:3.6138621999185303

The distance between p1 and p3 is:25.811818998280614

import java.util.Scanner;
/******start******/

class MyPoint {
private double x;
private double y;
public double getX(){
	return x;
}
public double getY(){
	return y;
}
public MyPoint(){
	x=0;
	y=0;
}
public MyPoint(double x,double y){
	this.x=x;
	this.y=y;
}

public double distance (MyPoint anotherPoint){
	return  Math.hypot((x-anotherPoint.getX()),(y-anotherPoint.getY()));
	}

public double distance (double x,double y){
	// d=Math.hypot((x-a),(y-b));
	return Math.sqrt((this.x-x)*(this.x-x)+(this.y-y)*(this.y-y));
	}

static public double distance(MyPoint p1,MyPoint p2){
	return Math.hypot((p1.getX()-p2.getX()), (p1.getY()-p2.getY()));
}
}

/******end******/
public class Main {
public static void main(String[] args) {
	Scanner scanner = new Scanner(System.in);
	System.out.println("Enter x-coordinates and y-coordinates:");
	double x = scanner.nextDouble();
	double y = scanner.nextDouble();
	MyPoint p1 = new MyPoint();
	MyPoint p2 = new MyPoint(12.6, 25.5);
	MyPoint p3 = new MyPoint(x, y);
	System.out.println("The distance between p1 and p2 is:" + MyPoint.distance(p1, p2));
	System.out.println("The distance between p2 and p3 is:" + p3.distance(p2.getX(), p2.getY()));
	System.out.println("The distance between p1 and p3 is:" + p3.distance(p1));
	scanner.close();
}
}

4.en_ 2017_ sw_ p2_004 Design a class Fan

Design a class named Fan to represent a fan. The class contains:

■ Three int constants named STOP, SLOW and FAST with the values 0, 1, and 2 to denote the fan speed.

■ A private String data field named number that specifies the number of the fan.

■ A private int data field named speed that specifies the speed of the fan.

■ A private boolean data field named on that specifies whether the fan is on.

■ A private double data field named radius that specifies the radius of the fan.

■ A string data field named color that specifies the color of the fan.

■ The setter and getter methods for data fields number, speed, radius and on. If the fan is turn on set speed is SLOW and if the fan is turn off then set speed is STOP.

■ A no-arg constructor Fan() that creates a default fan. It is off and it’s default number is “#1”, default speed is STOP, default radius is 5 and default color is white

■ A constructor Fan(String,double,String) that creates a Fan object for the specified number, radius and color, the fan is off and speed is STOP.

■ A method named toString() that returns a string description for the fan. If the fan is on, the method returns the fan number, speed, color, and radius in one combined string. If the fan is not on, the method returns the string “xxx fan is off” in one combined string, xxx is the fan’s number.

■ Since the Main class can not be modified so you define the Fan class must base on the invoking method code in the main method.

output example:

Enter the number, radius and color to create a fan:

#3 10 blue

#1 fan is off

#3 fan speed is 1 color is blue radius is 10.0

#1 fan speed is 1 color is white radius is 5.0

#3 fan speed is 2 color is blue radius is 10.0

程式碼如下:

import java.util.Scanner;
/******start******/

class Fan{
public static final int STOP=0;
public static final int SLOW=1;
public static final int FAST=2;

private String number;
private int speed;
private boolean on;
private double radius;
public String color;

public int getSpeed() {
	  return speed;
	  }
public void setSpeed(int speed) {
	  this.speed = speed;
	}
public boolean isOn() {
	  return on;
	}
public void setOn(boolean on) {
	  this.on = on;
	}
public double getRadius() {
	  return radius;
	}
public void setRadius(double radius) {
	  this.radius = radius;
	}
public String getColor() {
	  return color;
	}
public void setColor(String color) {
	  this.color = color;
	}

Fan(){
	number="#1";
	speed=SLOW;
	radius=5;
	color="white";
}
Fan(String number,double radius,String color){
	this.number=number;
	this.radius=radius;
	this.color=color;
	speed=SLOW;
}

public String toString() {
	  if(on==true){
	   return number+" fan speed is "+speed+" color is "+color+" radius is "+radius;
	  }
	  else{
	   return number+" fan is off";
	  }
}
}

/******end******/
public class Main {
public static void main(String[] args) {
	// TODO Auto-generated method stub
	Scanner scanner = new Scanner(System.in);
	System.out.println("Enter the number, radius and color to create a fan:");
	String number = scanner.next();
	double radius = scanner.nextDouble();
	String color = scanner.next();
	Fan fan1 = new Fan();
	Fan fan2 = new Fan(number,radius, color);
	fan2.setOn(true);
	System.out.println(fan1.toString());
	System.out.println(fan2.toString());
	fan1.setOn(true);
	fan2.setSpeed(Fan.FAST);
	System.out.println(fan1.toString());
	System.out.println(fan2.toString());
}
}

5.en_ 2017_ sw_ p2_005 Design a RegulaiPolygon

In an n-sided regular polygon(n面規則多邊形), all sides have the same length and all angles have the same degree (i.e., the polygon(多邊形) is both equilateral-等邊 and equiangular-等角). Design a class named RegularPolygon (規則多邊形) that contains:

■ A private int data field named n that defines the number of sides in the polygon with default value 3.

■ A private double data field named side that stores the length of the side with default value 2.

■ A private double data field named x that defines the x-coordinate(x座標) of the polygon’s center with default value 0.

■ A private double data field named y that defines the y-coordinate(y座標) of the polygon’s center with default value 0.

■ A no-arg constructor RegularPolygon() that creates a regular polygon with default values.

■ A constructor RegularPolygon(int,double) that creates a regular polygon with the specified number of sides and length of side, centered at (0, 0).

■ A constructor RegularPolygon(int,double,double,double) that creates a regular polygon with the specified number of sides, length of side, and x-and y-coordinates.

■ The setter and getter methods for all data fields.

■ The method getPerimeter() that returns the perimeter of the polygon.

■ The method getArea() that returns the area of the polygon. The formula for computing the area of a regular polygon is:

■ Since the Main class can not be modified so you define the RegularPolygon class must base on the invoking method code in the main method.

Output examples:

Enter the n, side, x-coordinate and y-coordinate to create a RegularPolygon object:

5 3 12 20

Polygon 1 x-coordinate: 0.0 y-coordinate: 0.0

Polygon 1 perimeter: 6.0

Polygon 1 area: 1.7320508075688779

Polygon 2 n: 3

Polygon 2 side: 5.0

Polygon 2 perimeter: 15.0

Polygon 2 area: 10.825317547305486

Polygon 3 perimeter: 15.0

Polygon 3 area: 15.484296605300703

import java.util.Scanner;
/******start******/

class RegularPolygon{
//numbers of side
private int n = 3;
//length of side
private double side = 2;
//座標
private double x = 0;
private double y = 0;

RegularPolygon() {
}
RegularPolygon(int n,double side){
	this.n = n;
	this.side = side;
}
RegularPolygon(int n,double side,double x,double y){
	this.n = n;
	this.side = side;
	this.x = x;
	this.y = y;
}
public int getN() {
	return n;
}
public void setN(int n) {
	this.n = n;
}
public double getSide() {
	return side;
}
public void setSide(double side) {
	this.side = side;
}
public double getX() {
	return x;
}
public void setX(double x) {
	this.x = x;
}
public double getY() {
	return y;
}
public void setY(double y) {
	this.y = y;
}
public double getPerimeter() {
	return side * n;
}
public double getArea() {
	double temp = this.n * Math.pow(this.side,2);
	double temp1 = Math.tan(Math.PI / this.n);
	return temp / (4 * temp1);
}
}

/******end******/
public class Main {
public static void main(String[] args) {
	Scanner scanner = new Scanner(System.in);
	System.out.println("Enter the n, side, x-coordinate and y-coordinate to create a RegularPolygon object:");
	int n = scanner.nextInt();
	double side = scanner.nextDouble();
	double x = scanner.nextDouble();
	double y = scanner.nextDouble();
	RegularPolygon polygon1 = new RegularPolygon();
	RegularPolygon polygon2 = new RegularPolygon(3, 5);
	RegularPolygon polygon3 = new RegularPolygon(n, side, x, y);
	System.out.println("Polygon 1 x-coordinate: " + polygon1.getX() + " y-coordinate: " + polygon1.getY());
	System.out.println("Polygon 1 perimeter: " + polygon1.getPerimeter());
	System.out.println("Polygon 1 area: " + polygon1.getArea());
	System.out.println("Polygon 2 n: " + polygon2.getN());
	System.out.println("Polygon 2 side: " + polygon2.getSide());
	System.out.println("Polygon 2 perimeter: " + polygon2.getPerimeter());
	System.out.println("Polygon 2 area: " + polygon2.getArea());
	System.out.println("Polygon 3 perimeter: " + polygon3.getPerimeter());
	System.out.println("Polygon 3 area: " + polygon3.getArea());
}
}

6.en_ 2017_ p1_sw_ 006 DEsign a class namedQuadraticEquation

此題寫出來編譯之後,只有60分,請大神賜教!

Design a class named QuadraticEquation for a quadratic equation(二次方程):ax2+bx+c = 0

The class contains:

■ Three private double data fields a, b, and c that represent three coefficients(三個係數a, b和c).

■ A constructor for the arguments for a, b, and c, The value of a can not be zero.

■ Three getter methods for a, b, and c.

■ A method named getDiscriminant() that returns the value of discriminant(判別式), which is:b2 - 4ac.

■ The methods named getRoot1() and getRoot2() for returning two roots of the equation. The formulas to calculate roots are following:

These calculate roots methods are useful only if the discriminant is nonnegative.(非負) Let these methods return 0 if the discriminant is negative. And if discriminant is zero then just has one root(r1). If discriminant bigger than zero there has two roots.

■ Since the Main class can not be modified so you define the RegularPolygon class must base on the invoking method code in the main method.

Output examples:

Output examples1:

Enter a, b, c:

1 2 1

The root is -1.0

Output examples2:

Enter a, b, c:

2 9 4

The roots are 10.0 and -14.5

Output examples3:

Enter a, b, c:

8 2 7

The equation has no roots

import java.util.Scanner;
/******start******/

class QuadraticEquation{
private double a,b,c;
public QuadraticEquation(double a,double b,double c){
	if(a!=0){
		this.a=a;
		this.b=b;
		this.c=c;
	}
}

public double getA(){
	return a;
}
public double getB(){
	return b;
}
public double getC(){
	return c;
}

public double getDiscriminant(){
	return (b*b-4*a*c);
}

public double getRoot1(){
	return (-b+Math.sqrt(b*b-4*a*c))/(2*a);
}
public double getRoot2(){
	return (-b-Math.sqrt(b*b-4*a*c))/(2*a);
}

}

/******end******/
public class Main {
public static void main(String[] args) {
	Scanner input = new Scanner(System.in);
	System.out.print("Enter a, b, c: ");
	double a = input.nextDouble();
	double b = input.nextDouble();
	double c = input.nextDouble();
	QuadraticEquation equation = new QuadraticEquation(a, b, c);
	double discriminant = equation.getDiscriminant();
	if (discriminant < 0) {
		System.out.println("The equation has no roots");
	} else if (discriminant == 0) {
		System.out.println("The root is " + equation.getRoot1());
	} else // (discriminant >= 0)
	{
		System.out.println("The roots are " + equation.getRoot1() + " and " + equation.getRoot2());
	}
}
}

7.en_ 2017_ p2_ 001_ ex The REctangle class

Following the example of the Circle class, design a class named Rectangle to represent a rectangle. The class contains:

■ Two double data fields named width and height that specify the width and height of the rectangle. The default values are 1 for both width and height.

■ A no-arg constructor that creates a default rectangle.

■ A constructor that creates a rectangle with the specified width and height.

■ A method named getArea() that returns the area of this rectangle.

■ A method named getPerimeter() that returns the perimeter.

Creates two Rectangle objects in the main method of the Main class—. Display the width, height, area, and perimeter of each rectangle in this order:

The program output example when it runs in Eclipse:

import  java.util.Scanner;
/******start******/
//宣告定義Rectangle類,去掉public關鍵字。

class Rectangle{
public double width;
public double height;
public Rectangle(){
	width=1.0;
	height=1.0;
}
public Rectangle(double width,double height){
	this.width=width;
	this.height=height;
}
public double getArea(){
	return width*height;
}
public double getPermeter(){
	return (width+height)*2;
}
}

/******end******/
public class Main{
public static void main(String[] args){
       Scanner scanner = new Scanner(System.in);
       System.out.println("Enter the width and height of  a rectangle:");
       double w= scanner.nextDouble();
      double h = scanner.nextDouble();
    /******start******/
     //使用預設構造方法建立第一個Rectangle物件
       Rectangle rec1=new Rectangle();
  

      //使用使用者輸入的高(h)和寬(w)的資料建立第二個Rectangle物件。
       Rectangle rec2=new Rectangle(w,h);


      //分別輸出這兩個Rectangle物件的寬、高、面積和周長四個值,每個物件的四個值之間用1個空格分開。每個物件的四個數值單獨為一行
      //output example:  sa
      //1.0 1.0 1.0 4.0
      //3.0 5.0 15.0 16.0

       System.out.println(rec1.width+" "+rec1.height+" "+rec1.getArea()+" "+rec1.getPermeter());
       System.out.println(rec2.width+" "+rec2.height+" "+rec2.getArea()+" "+rec2.getPermeter());


	 //使用預設構造方法建立第一個Rectangle物件
     //使用使用者輸入的高(h)和寬(w)的資料建立第二個Rectangle物件。



      //分別輸出這兩個Rectangle物件的寬、高、面積和周長四個值,每個物件的四個值之間用1個空格分開。每個物件的四個數值單獨為一行
     //output example:  
      //1.0 1.0 1.0 4.0
    //  3.0 5.0 15.0 16.0

         /******end******/
}
}