Java期中檢測

在這裏插入圖片描述
一.判斷題
1-1
A final class can be extended. (F)

1-2
An abstract class can extend an interface. (F)

1-3
子類如果想使用父類的構造方法,必須在子類的構造方法中使用,並且必須使用關鍵字super來表示,而且super必須是子類構造方法中的頭一條語句。(T)

(1 分)
1-4
Java語言中的數組元素下標總是從0開始,下標可以是整數或整型表達式。 (T)

1-5
java語言中不用區分字母的大寫小寫。 (F)

1-6
Java的各種數據類型佔用固定長度,與具體的軟硬件平臺環境無關。 (T)

(1 分)
1-7
一個Java源文件中可以有多個類,但只能有一個類是public的。 (T)

二.選擇題
1.編譯Java源程序文件將產生相應的字節碼文件,這些字節碼文件的擴展名爲( B)。

A.byte
B.class
C.html
D.exe

2.Which of the following are Java keywords? ( B)

A.array
B.boolean
C.Integer
D.Long

3.下面哪一句是關於非靜態的內部類正確的描述?(B)

A.It must implement an interface.
B.It can access private instance variables in the enclosing object.
C.It is accessible from any other class.
D.It can only be instantiated in the enclosing class.
E.It must be final if it is declared in a method scope.

4.Java語言具有許多優點和特點,哪個反映了Java程序並行機制的特點?(B )

A.安全性
B.多線性
C.跨平臺
D.可移植

5.Suppose A is an inner class in Test. A is compiled into a file named (B)
(此題被我錯誤選擇成了A…)
A.ATest.classB.TestTest.class B.TestA.class
C.A.class
D.Test&A.class

6.A派生出子類B,B派生出子類C,對於如下Java源代碼正確的說法是(D)。

1.    A  a0 =new  A(); 
2.    A  a1 =new  B(); 
3.    A  a2 =new  C(); 

A.只有第1行能通過編譯
B.第1、2行能通過編譯,但第3行編譯出錯
C.第1、2、3行能通過編譯,但第2、3行運行時出錯
D.第1行、第2行和第3行的聲明都是正確的

三.程序填空

1.`求解圓柱體的體積
import java.util.Scanner;
class Circle 
{
      private double radius;
      public Circle(double radius) {this.radius = radius;}
      public double getRadius() {
			return radius;
	}
      public void setRadius(double radius) {
			this.radius = radius;
		}
      public double getArea() {return 
			Math.PI*radius*radius;
 	}
}
class Cylinder extends Circle{
  		 double height =100;
  		 public Cylinder(double radius,double height) {super(radius); this.height = height ;}
  		 public double getVolumn() {
  				 return getArea()  * height;
 		 }
}
public class Main{
 		public static void main(String[] args) {
      		Scanner sc = new Scanner(System.in);
       		double height = sc.nextDouble();
       		double radius = sc.nextDouble();
        	Cylinder obj = new Cylinder(radius, height);
        	System.out.printf("Cylinder obj Volumn is %.2f",obj.getVolumn() );
  		}
}`
2.
(檢驗密碼)一些網站設定了一些制定密碼的規則。編寫一個方法,檢驗一個字符串是否合法的密碼。假設密碼規則如下: 密碼必須至少有8個字符。 密碼只能包含字母和數字。 密碼必須至少有2個數字。 請編寫一個程序,提示用戶輸入密碼,如果改密碼符合規則就顯示“Valid password”,否則顯示“Invalid password”

public class Main {
	  public static void main(String[] args) {
	    // Prompt the user to enter a password
	    java.util.Scanner input = new java.util.Scanner(System.in);
	    //System.out.print("Enter a string for password: ");
	    String s = input.nextLine();
	    if (
isValidPassword(s)
) {
	      System.out.println("Valid password");
	    }
	    else {
	      System.out.println("Invalid password");
	    }
	  }
	  /** Check if a string is a valid password */
	  public static boolean isValidPassword(String s) {
	    // Only letters and digits?
	    for (int i = 0; i < s.length(); i++) {
	      if (
!Character.isLetter(s.charAt(i))
) && 
	          !Character.isDigit(s.charAt(i)))
	        return false;
	    }
	    
	    // Check length
	    if (
s.length()
 < 8)
	      return false;
	    
	    // Count the number of digits
	    int count = 0;
	    for (int i = 0; i < s.length(); i++) {
	      if (
Character.isDigit(s.charAt(i))
)
	        count++;
	    }
	    
	    if (count >= 2)
	      return true;
	    else 
	      return false;
	  }
	}

四.函數題

  1. 6-1 定義一個股票類Stock (10 分)
    定義一個名爲Stock的股票類,這個類包括:一個名爲symbol的字符串數據域表示股票代碼。一個名爲name的字符串數據域表示股票名稱。一個名爲previousClosingPrice的double數據域,它存儲前一日的股票交易價格。一個名爲currentPrice數據域,它存儲當前的股票交易價格。創建一個有特定代碼和名稱的股票的構造方法。一個名爲getChangePercent()方法返回從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%
class Stock{
    String symbol;
    String name;
    double previousClosingPrice;
    double currentPrice;
    // public Stock(){}
     public Stock(String newSymbol,String newName){
        symbol=newSymbol;
        name=newName;
    }
    public double changePercent(){
        return (currentPrice-previousClosingPrice)/previousClosingPrice;
    }
}
 **從抽象類shape類擴展出一個正n邊形 (10 分)**
在一個正n邊形(Regular Polygon)中,所有邊的邊長都相等,且所有角的度數相同(即這個多邊形是等邊、等角的)。請從下列的抽象類shape類擴展出一個正n邊形類RegularPolygon,這個類將正n邊形的邊數n和邊長a作爲私有成員,類中包含初始化邊數n和邊長a的構造方法。 public abstract class shape {// 抽象類 public abstract double getArea();// 求面積 public abstract double getPerimeter(); // 求周長 } 計算正n邊形的面積公式爲: Area=n×a×a/(tan((180度/n))×4);

**類名:RegularPolygon**
**裁判測試程序樣例:**
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位小數
        int n=input.nextInt();
        double side = input.nextDouble();

        shape rp = new  RegularPolygon(n,side);

        System.out.println(d.format(rp.getArea()));
        System.out.println(d.format(rp.getPerimeter()));
        input.close();
    }
}
**輸入樣例:**
5
7
**輸出樣例:**
84.3034
35
class RegularPolygon extends shape{
    private double a=0;
    private int n=0;
    public RegularPolygon(int n,double side){
        a=side;
        this.n=n;
    }
    public double getArea(){
        return n*a*a/(Math.tan(Math.toRadians(180/n))*4);
    }
    public double getPerimeter(){
        return n*a;
    }
}

六.編程題

7-1 wind-chill temperature (10 分)
(Science: wind-chill temperature) How cold is it outside? The temperature alone is not enough to provide the answer. Other factors including wind speed, relative humidity, and sunshine play important roles in determining coldness outside. In 2001, the National Weather Service (NWS) implemented the new wind-chill temperature to measure the coldness using temperature and wind speed. The formula is given as follows: windCold = 35.74 + 0.6215 x fahrenheit - 35.75 x Math.pow(speed, 0.16) + 0.4275 x fahrenheit x Math.pow(speed, 0.16);

input style :
Enter three value for the Fahrenheit and wind speed miles per hour.

output style:
Output the wind chill index.

input sample:
5.3
6
output sample:
The wind chill index is -5.56707
import java.util.*;
public class Main{
    public static void main(String[]args){
        Scanner in=new Scanner(System.in);
        double f=in.nextDouble();
        double v=in.nextDouble();
        double w=35.74 + 0.6215 * f - 35.75 * Math.pow(v, 0.16) + 0.4275 *f *Math.pow(v, 0.16);
        System.out.println("The wind chill index is "+w);
    }
}

7-2 Remove duplicates (10 分)
(Remove duplicates) Write a method that removes the duplicate elements from an array list of integers using the following header: public static void removeDuplicate(ArrayList list) Write a test program that prompts the user to enter n integers to a list ,After sort the distinct intergers and displays the distinct integers separated by exactly one space.

input style :
Input the number n for the length of the array in the first line, and the next line input n integers for the array..

output style:
Displays the distinct integers separated by exactly one space

input sample:
5
32 43 32 22 22
output sample:
32 43 22
import java.util.*;
public class Main {
	
	public static void main(String[] args) {
		Scanner in=new Scanner(System.in);
		int num=in.nextInt();
		int []a=new int [num];
		int []b=new int [num];
		int flag=1;
		int count=0;
		int index=0;
		for(int i=0;i<num;i++){
			flag=1;
			int temp=in.nextInt();
			index++;
			for(int j=0;j<index;j++){
				if(temp==a[j]){
					flag=0;
					break;
				}
			}
			if(flag==1){
				a[i]=temp;
				b[count]=temp;
				count++;
			}
			else{
				continue;
			}
		}
        for(int m=0;m<count;m++){
				System.out.print(b[m]+" ");
			}
	}
}
7-3 找出最大的對象 (10 分)
(找出最大的對象)編寫一個方法,返回對象數組中最大的對象。方法簽名如下: public static Object max(Comparable[] a) 所有對象都是Comparable接口的實例。對象在數組中的順序是由compareTo方法決定的。 編寫測試程序,從鍵盤輸入5個字符串和5個整數,創建一個由5個字符串構成的數組、一個由5個整數構成的數組。找出數組中最大的字符串、整數並輸出。

輸入格式:
輸入 Xi'an (輸入5個字符串,每行一個) Beijing ShangHai GuangZhou ShenZhen 8 9 12 7 6 (輸入5個整數,以空格分隔)

輸出格式:
輸出 Max string is Xi'an (輸出最大的字符串) Max integer is 12 (輸出最大的整數)

輸入樣例:
France
Japan
German
China
India
6 34 89 168 53
輸出樣例:
Max string is Japan
Max integer is 168
import java.util.*;
public class Main {
	private static Object max1(String[]b){
		Arrays.sort(b);
		return b[4];
	}
	private static Integer max2(int[]b1){
		Arrays.sort(b1);
		return b1[4];
	}
	public static void main(String[] args) {
		String []a=new String[5];
		int []b=new int [5];
		Scanner in=new Scanner(System.in);
		for(int i=0;i<5;i++){
			a[i]=in.next();
		}
		for(int i=0;i<5;i++){
			b[i]=in.nextInt();
		}
		System.out.println("Max string is "+max1(a));
		System.out.println("Max integer is "+max2(b));
	}

}
7-4 查找電話號碼 (10 分)
文件phonebook1.txt中有若干聯繫人的姓名和電話號碼。 高富帥 13312342222 白富美 13412343333 孫悟空 13512345555 唐三藏 13612346666 豬悟能 13712347777 沙悟淨 13812348888 請你編寫一個簡單的通信錄程序,當從鍵盤輸入一個姓名時查找到對應的電話號碼並輸出。如果沒找到則顯示Not found. 由於目前的自動裁判系統暫時不能支持用戶讀入文件,我們編寫程序從鍵盤輸入文件中的姓名和電話號碼,當輸入的名字爲noname時,表示結束。noname後面有一個名字,需要查找其對應的電話號碼。

輸入格式:
高富帥 13312342222 白富美 13412343333 孫悟空 13512345555 唐三藏 13612346666 豬悟能 13712347777 沙悟淨 13812348888 noname (表示結束) 唐三藏 (需要查找此人的電話號碼)

輸出格式:
13612346666 (輸出對應的電話號碼)

輸入樣例:
白富美 13412343333
孫悟空 13512345555
唐三藏 13612346666
豬悟能 13712347777
沙悟淨 13812348888
noname
白骨精
輸出樣例:
Not found.
import java.text.*;
import java.util.*;
public class Main{
    public static void main(String[]args){
        Scanner in=new Scanner(System.in);
        HashMap<String,String>map=new HashMap<String,String>();

        while(true){
        	String s=in.next();
            if(s.equals("noname")){
             
                break;
            }
            else{
            	String t=in.next();
            	map.put(s, t);
            }
        }
        String ts=in.next();
        if(map.containsKey(ts)){
            System.out.println((map.get(ts)));
        }
        else{
            System.out.println("Not found.");
        }
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章