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.A T e s t . c l a s s B . T e s t Test.class B.Test Test.classB.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.");
        }
    }
}

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值