這學期學校開了Java面向對象程序設計,期末考試是在PTA上進行的,今天就把期末考試前老師在PTA上放的函數題總結一下,希望對大家有所幫助。個人能力有限,如有錯誤,煩請指正。
- 從抽象類shape類擴展出一個圓形類Circle(10 分)
請從下列的抽象類shape類擴展出一個圓形類Circle,這個類圓形的半徑radius作爲私有成員,類中應包含初始化半徑的構造方法。
public abstract class shape { // 抽象類
public abstract double getArea(); // 求面積
public abstract double getPerimeter(); // 求周長
}
主類從鍵盤輸入圓形的半徑值,創建一個圓形對象,然後輸出圓形的面積和周長。保留4位小數。
圓形類名
Circle
裁判測試程序樣例:
import java.util.Scanner;
import java.text.DecimalFormat;
abstract class shape {// 抽象類
/* 抽象方法 求面積 */
public abstract double getArea( );
/* 抽象方法 求周長 */
public abstract double getPerimeter( );
}
/* 你提交的代碼將被嵌入到這裏 */
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
DecimalFormat d = new DecimalFormat("#.####");// 保留4位小數
double r = input.nextDouble( );
shape c = new Circle(r);
System.out.println(d.format(c.getArea()));
System.out.println(d.format(c.getPerimeter()));
input.close();
}
}
輸入樣例:
3.1415926
輸出樣例:
31.0063
19.7392
答案:
//從抽象類shape類擴展出一個圓形類Circle
class Circle extends shape {
private double radius;
Circle(double r) {
this.radius = r;
}
public double getArea() {
return Math.PI * radius * radius;
}
public double getPerimeter() {
return 2 * Math.PI * radius;
}
}
2.創建一個正六邊形類實現接口IShape(10 分)
創建一個正六邊形(regular hexagon)RHexagon類,實現下列接口IShape。RHexagon類將正六邊形的邊長作爲私有成員,類中包含初始化這個值的構造方法。
interface IShape { // 接口
double getArea(); // 求面積
double getPerimeter();// 求周長
}
請編程從鍵盤輸入正六邊形的邊長值,創建一個正六邊形對象,然後輸出正六邊形的面積和周長。保留4位小數。
正六邊形類名:
RHexagon
裁判測試程序樣例:
import java.util.Scanner;
import java.text.DecimalFormat;
interface IShape {
double getArea();
double getPerimeter();
}
//你提交的代碼將被嵌入到這裏
public class Main {
public static void main(String[] args) {
DecimalFormat d = new DecimalFormat("#.####");
Scanner input = new Scanner(System.in);
double a = input.nextDouble();
IShape r = new RHexagon (a);
System.out.println(d.format(r.getArea()));
System.out.println(d.format(r.getPerimeter()));
input.close();
}
}
輸入 16.8 (邊長) 輸出 733.281 (輸出的面積) 100.8 (輸出的周長)
輸入樣例:
5
輸出樣例:
64.9519
30
答案:
//創建一個正六邊形類實現接口IShape
class RHexagon implements IShape {
private double r;
RHexagon(double r) {
this.r = r;
}
public double getArea() {
return (3 * Math.sqrt(3)) / 2 * r * r;
}
public double getPerimeter() {
return (6 * r);
}
}
3.從抽象類shape類擴展出一個正五邊形類(10 分)
從下列的抽象類shape類擴展出一個正五邊形(regular pentagon)類RPentagon,這個類將正五邊形的邊長作爲私有成員,類中包含初始化這個值的構造方法。
public abstract class shape { // 抽象類
public abstract double getArea(); // 求面積
public abstract double getPerimeter(); // 求周長
}
計算正五邊形的面積公式爲: S=(1/4)a^2√(25+10√5)
正五邊形類名:
RPentagon
裁判測試程序樣例:
import java.util.Scanner;
import java.text.DecimalFormat;
abstract class shape {// 抽象類
/* 抽象方法 求面積 */
public abstract double getArea();
/* 抽象方法 求周長 */
public abstract double getPerimeter();
}
/* 你提交的代碼將被嵌入到這裏 */
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
DecimalFormat d = new DecimalFormat("#.####");// 保留4位小數
double side = input.nextDouble();
shape rp = new RPentagon(side);
System.out.println(d.format(rp.getArea()));
System.out.println(d.format(rp.getPerimeter()));
input.close();
}
}
輸入樣例:
16.8
輸出樣例:
485.5875
84
答案:
//從抽象類shape類擴展出一個正五邊形類
class RPentagon extends shape {
RPentagon(double r) {
a = r;
}
public double getArea() {
return 0.25 * a * a * Math.sqrt(25 + 10 * Math.sqrt(5));
}
public double getPerimeter() {
return 5 * a;
}
private double a;
}
4.設計一個矩形類Rectangle(10 分)
設計一個名爲Rectangle的類表示矩形。這個類包括: 兩個名爲width和height的double型數據域,它們分別表示矩形的寬和高。width和height的默認值都爲1. 一個無參構造方法。 一個爲width和height指定值的矩形構造方法。 一個名爲getArea()的方法返回這個矩形的面積。 一個名爲getPerimeter()的方法返回這個矩形的周長。
類名爲:
Rectangle
裁判測試程序樣例:
import java.util.Scanner;
/* 你的代碼將被嵌入到這裏 */
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double w = input.nextDouble();
double h = input.nextDouble();
Rectangle myRectangle = new Rectangle(w, h);
System.out.println(myRectangle.getArea());
System.out.println(myRectangle.getPerimeter());
input.close();
}
}
輸入樣例:
3.14 2.78
輸出樣例:
8.7292
11.84
答案:
//設計一個矩形類Rectangle
class Rectangle {
Rectangle() {
width = 1;
height = 1;
}
Rectangle(double w, double h) {
width = w;
height = h;
}
public double getArea() {
return width * height;
}
public double getPerimeter() {
return 2 * (width + height);
}
private double width;
private double height;
}
5.定義一個股票類Stock(10 分)
定義一個名爲Stock的股票類,這個類包括:一個名爲symbol的字符串數據域表示股票代碼。一個名爲name的字符串數據域表示股票名稱。一個名爲previousClosingPrice的double數據域,它存儲前一日的股票交易價格。一個名爲currentPrice數據域,它存儲當前的股票交易價格。創建一個有特定代碼和名稱的股票的構造方法。一個名爲changePercent()方法返回從previousClosingPrice變化到currentPrice的百分比。
類名爲:
Stock
裁判測試程序樣例:
import java.util.Scanner;
/* 你提交的代碼將被嵌入到這裏 */
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String symbol1=input.next();
String name1=input.next();
Stock stock = new Stock(symbol1, name1);
stock.previousClosingPrice = input.nextDouble();
// Input current price
stock.currentPrice = input.nextDouble();
// Display stock info
System.out.println(stock.name+" Price Changed: " + stock.changePercent() * 100 + "%");
input.close();
}
}
輸入樣例:
002594
比亞迪
56.98
55.40
輸出樣例:
比亞迪 Price Changed: -2.77290277290277%
答案:
//定義一個股票類Stock
class Stock {
Stock(String s, String n) {
symbol = s;
name = n;
}
public double changePercent() {
return (currentPrice - previousClosingPrice) / previousClosingPrice;
}
public String symbol;
public String name;
public double previousClosingPrice;
public double currentPrice;
}
6.創建一個直角三角形類實現IShape接口(10 分)
創建一個直角三角形類(regular triangle)RTriangle類,實現下列接口IShape。兩條直角邊長作爲RTriangle類的私有成員,類中包含參數爲直角邊的構造方法。
interface IShape { // 接口
public abstract double getArea(); // 抽象方法 求面積
public abstract double getPerimeter(); // 抽象方法 求周長
}
直角三角形類的定義:
直角三角形類的構造函數原型如下:
RTriangle(double a, double b);
其中 a 和 b 都是直角三角形的兩條直角邊。
裁判測試程序樣例:
import java.util.Scanner;
import java.text.DecimalFormat;
interface IShape {
public abstract double getArea();
public abstract double getPerimeter();
}
/*你寫的代碼將嵌入到這裏*/
public class Main {
public static void main(String[] args) {
DecimalFormat d = new DecimalFormat("#.####");
Scanner input = new Scanner(System.in);
double a = input.nextDouble();
double b = input.nextDouble();
IShape r = new RTriangle(a, b);
System.out.println(d.format(r.getArea()));
System.out.println(d.format(r.getPerimeter()));
input.close();
}
}
輸入樣例:
3.1 4.2
輸出樣例:
6.51
12.5202
答案:
//創建一個直角三角形類實現IShape接口
class RTriangle implements IShape {
RTriangle(double a, double b) {
this.a = a;
this.b = b;
}
public double getArea() {
return 0.5 * a * b;
}
public double getPerimeter() {
return (a + b + Math.sqrt(a * a + b * b));
}
private double a;
private double b;
}