说明:

以下为过程化考试平台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 stubScanner 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 sideprivate int n = 3;//length of sideprivate 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.0System.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******/}}

exam平台Java试题阶段(二)相关推荐

  1. exam平台Java试题阶段(一)

    说明: 以下为过程化考试平台Java练习题,代码是自己写的,由于我的水平很有限,不免有不妥之处,欢迎指正赐教,谢谢! 1.en_ 2017_ p1_001 Convert Celsius to Fah ...

  2. Java校招笔试题-Java基础部分(二)

    导语   Java面试题集2021版 Java基础部分二 14.hashCode方法的作用? 15.写clone()方法时,通常都有一行代码,这行代码是什么? 16.静态变量和实例变量的区别? 17. ...

  3. Java笔试题(二)多选题

    1.下列属于jsp中注释的有 A.<%-- 与 --%>B. /C. /** 与 **/D. <!-- 与 --> 答案:AD 2. 按照学生平均成绩(avg_grade) 将 ...

  4. 面试题汇总二 Java 多线程篇

    前言 题目汇总来源 史上最全各类面试题汇总,没有之一,不接受反驳 面试题汇总一 Java 语言基础篇 面试题汇总二 Java 多线程篇 面试题汇总三 Java 集合篇 面试题汇总四 JVM 篇 面试题 ...

  5. Java实习生常规技术面试题每日十题Java基础(二)

    目录 1. JAVA 的反射机制的原理. 2.静态嵌套类(Static Nested Class)和内部类(Inner Class)的不同? 3.如何将String类型转化成Number类型. 4.什 ...

  6. Linux平台Java环境中文编码研究

    09年在原来公司做的一篇文章,现在共享出来. 此次研究主要针对Linux操作系统中Java环境下可能产生的中文乱码问题展开一些试验,目的在于寻求一套无乱码的解决方案. 此文档目的在于详细介绍<2 ...

  7. 华清远见-重庆中心-JAVA基础阶段技术总结

    系列文章目录 第一章 华清远见--重庆中心-JAVA基础阶段技术总结 第二章 文章目录 系列文章目录 文章目录 前言 一.关于java 1.发展历程 2.编程开发 3.java架构 4.java的特点 ...

  8. 华清远见—重庆中心——JAVA高级阶段知识点梳理

    华清远见-重庆中心--JAVA高级阶段知识点梳理 String字符串 String是一个类,属于数据类型中的引用类型.Java中所有使用""引起来的内容都是属于这个类的实例,称为字 ...

  9. 华为Java开发一面二面 附笔试(OD)

    华为Java开发一面二面(OD)4.3 面试的是1-3年工作经验的岗位 一面:45min     主要是问项目和平时用到的一些工具之类的,因为我写的比较少,所以基本上都是问的springboot和sp ...

最新文章

  1. 推荐一款好用的redis客户端工具
  2. 不能装载通讯模块。驱动程序安装不正确。
  3. 适配器模式coding
  4. 怎么配置iptv服务器信息,请配置iptv服务器信息
  5. java实现单链表常见操作,java面试题,java初级笔试题
  6. 这才是厉害程序员的标配!
  7. 基于线程池技术的web服务器
  8. linux wps 数学符号,2016版WPS数学符号如何输入
  9. 在VMware上安装Ubuntu详细教程
  10. c语言高斯法解方程,用C语言实现解线性方程组的高斯消去法
  11. 机器人感知与规划笔记 (7) - 行为架构 (Behavioral Architectures)
  12. 休假管理系统的问题描述与词汇表
  13. Vue项目安装依赖时 warning“unmet peer dependency“
  14. Visual C++ 6.0 ( VC 6 )带 SP6 中英文双语版 下载
  15. 网络舆情监测与分析研判工作如何高效做好的解决方案
  16. 用74HC165读8个按键状态
  17. C/C++实习面试(一)
  18. NTP时间同步服务器
  19. 「 理财与风险控制|养老系列 」交了N年的社保,退休能领多少钱
  20. 多屏幕ALV ZCL_ALV_MULTI

热门文章

  1. vim 替换文本指令
  2. android源码树结构介绍
  3. KVM-Virsh指令详解
  4. 华为设备ENSP基本命令简介
  5. Aspose 只更新页码和 更新域 UpdatePageNumbers
  6. HBase命令行基本操作
  7. Linux lseek函数
  8. 【读书笔记】YARN架构设计与实现原理
  9. python生成图片
  10. rmi反序列化导致rce漏洞修复_RMI反序列化漏洞分析