2021-2022(1)程序设计JAVA 考前练习

练习平台

题目1

设计一个类Validation,包含公有的类方法String ValidateUsername(String username),实现验证用户名是否满足规范(用户名由8-16位字符组成,字符可以是数字、字母、下划线),如果满足规范,返回Valid Username,否则返回Invalid Username;公有的类方法 String ValidatePWD(String pwd),实现验证密码是否满足规范(密码由6位数字组成),如果满足规范,返回Valid PWD,否则返回Invalid PWD。

说明:需要引入Scanner类

例如:

测试输入Result
Scanner read=new Scanner(System.in);
System.out.printf(“please input username and password:\n”);
System.out.printf("%s\n",Validation.ValidateUsername(read.next()));
System.out.printf("%s\n",Validation.ValidatePWD(read.next()));
lixiaoxu123 abc123please input username and password:
Valid Username
Invalid PWD

答案:

import java.util.Scanner;
class Validation{
	public static String ValidateUsername(String username){
		String regex = "\\w+";
		if(username.matches(regex) && username.length() >= 8 && username.length() <= 16){
			return "Valid Username";
		}
		else{
			return "Invalid Username";
		}
	}
	public static String ValidatePWD(String pwd){
		String reg = "\\d{6}";
		if(pwd.matches(reg)){
			return "Valid PWD";
		}
		else{
			return "Invalid PWD";
		}
	}
}

题目2

设计一个类A,该类实现了Runnable接口,主要功能是逆序输出前n个小写英文字母。定义类A的相应成员变量和方法,完成以上功能。

说明:需要引入Scanner类。

例如:

测试输入Result
Scanner read=new Scanner(System.in);
int n=read.nextInt();
A a=new A(n);
Thread thread=new Thread(a);
thread.start();
6fedcba

答案:

import java.util.Scanner;
class A implements Runnable{
	int n;
	A(int a){
		n = a;
	}
	public void run(){
		for(int i = 97 + n - 1; i >= 97; i--){
			System.out.printf("%c", i);
		}
	}
}

题目3

设计一个类MyArrayList,包含私有的ArrayList对象成员;公有的构造方法,实现将键盘上输入的10个整数,添加到ArrayList对象中;公有的方法void removeDuplicate(),实现将ArrayList对象成员中的重复元素去掉;公有的方法 void show(),实现输出ArrayList对象成员中的所有元素(输出的每个元素后面用一个空格隔开)。

说明:需要引入Scanner类和ArrayList类。

例如:

测试输入Result
System.out.printf(“Enter ten integers: \n”);
MyArrayList list=new MyArrayList();
list.removeDuplicate();
System.out.printf("The distinct integers are: ");
list.show();
7 4 6 7 3 8 5 4 6 2Enter ten integers:
The distinct integers are: 7 4 6 3 8 5 2

答案:

import java.util.*;
class MyArrayList{
	private ArrayList<Integer> list = new ArrayList<Integer>();//不能只写private ArrayList<Integer> list;
	public MyArrayList(){
		Scanner reader = new Scanner(System.in);
		String s = reader.nextLine();
		String sd[] = s.split(" ");
		int d[] = new int[10];
		for(int i = 0; i < 10; i++){
			d[i] = Integer.parseInt(sd[i]);
			list.add(d[i]);
		}
	}
	public void removeDuplicate(){
		list = new ArrayList<Integer>(new LinkedHashSet<Integer>(list));
	}
	public void show(){
		int l = list.size();
		for(int i = 0; i < l; i++){
			if(i != l - 1){
				System.out.print(list.get(i) + " ");
			}
			else{
				System.out.print(list.get(i));
			}
		}
	}
}

题目4

某同学选修的课程及成绩保存到了/home/java/course.txt文件中。定义一个类A,包含公有的方法double avg_Grade(int n),返回该同学前n门课的平均分,输出效果如示例所示。

说明:
①创建文件流对象时,用到的路径是 “/home/java/course.txt”
②course.txt文件中课程及成绩的保存形式如下图所示:(课程名和成绩之间用一个空格分隔)
请添加图片描述

例如:

测试Result
A a=new A();
System.out.printf("%.2f", a.avg_Grade(2));
89.00

答案:

import java.io.*;
class A{
	String f = "/home/java/course.txt";
	public double avg_Grade(int n){
		double sum = 0;
		try{
			BufferedReader r = new BufferedReader(new FileReader(f));
			String l = r.readLine();			
			for(int i = 0; i < n; i++){
				String s[] = l.split(" ");
				double score = Double.parseDouble(s[1]);
				sum += score;
				l = r.readLine();
			}
		}
		catch(IOException e){}
		return sum/n;
	}
}

题目5

现有类Phone代码如下:

abstract class Phone{
        protected double price; //手机单价
        protected int number; //手机数量
        public Phone( double p, int n){
                price=p;
                 number=n;
        }
        abstract double getPrice();
}

现要求实现Phone的两个子类IPhone和Huawei,覆盖getPrice方法。手机折扣规则为:

IPhone:
总金额<6000,九五折
6000≤总金额<15000,九折
总金额≥15000,八五折
Huawei:
总金额<4000,九五折
4000≤总金额<10000,九折
总金额≥10000,八五折

说明:抽象类Phone已嵌入到程序中,提交答案的时候,不要再提交该类。

例如:

测试Result
Phone p=new IPhone(5500,3);
System.out.printf("%.2f\n",p.getPrice());
p=new Huawei(4200,2);
System.out.printf("%.2f\n",p.getPrice());
14025.00
7560.00

答案:

class IPhone extends Phone{
	IPhone(double p, int n){
		super(p, n);		
	}
	double getPrice(){
		double p = price*number;
		if(p < 6000){
			return 0.95*p;
		}
		else if(p >= 6000 && p < 15000){
			return 0.9*p;
		}
		else{
			return 0.85*p;
		}
	}
}
class Huawei extends Phone{
	Huawei(double p, int n){
		super(p, n);		
	}
	double getPrice(){
		double p = price*number;
		if(p < 4000){
			return 0.95*p;
		}
		else if(p >= 4000 && p < 10000){
			return 0.9*p;
		}
		else{
			return 0.85*p;
		}
	}
}

题目6

设计一个类Substring,包含公有的类方法int countSubstring(String originalString, String subString),实现查找源字符串中子串出现的次数,如果没有该子串,则返回0。其中,该方法的第1个参数是源字符串,第2个参数是要查找的子串。

例如:

测试Result
String originalstring=“A man may usually be known by the books he reads as well as by the company he keeps.”;
String substring=“by”;
int count=Substring.countSubstring(originalstring, substring);
System.out.printf("%d\n",count);
2

答案:

class Substring{
	public static int countSubstring(String originalString, String subString){
		String s = originalString.replaceAll(subString, "");
		return (originalString.length() - s.length())/subString.length();
	}
}

题目7

BMI指数也就是体重指数,是体重除以身高的平方。体重的单位是公斤,身高的单位是米。BMI<18.5,偏瘦(Underweight);18.5≤BMI<24,正常(Normal);24≤BMI<28,超重(Overweight);BMI≥28肥胖(Obese)。

设计一个BMI类,包含私有的数据成员身高(Height)和体重(Weight);公有的构造方法对数据成员初始化,公有的方法double getBMI()计算并返回体重指数,公有的方法String getStatus()返回体重状态。

说明:BMI=体重/(身高*身高)

例如:

测试Result
BMI bmi=new BMI(1.7,85);
System.out.printf(“The BMI is %.1f %s\n”, bmi.getBMI(),bmi.getStatus());
The BMI is 29.4 Obese

答案:

class BMI{
	private double Height;
	private double Weight;
	public BMI(double h, double w){
		Height  = h;
		Weight = w;	
	}	
	public double getBMI(){
		return Weight / (Height * Height);
	}	
	public String getStatus(){
		if(Weight / (Height * Height) < 18.5){
			return "Underweight";
		}
		else if(Weight / (Height * Height) >= 18.5 && Weight / (Height * Height) < 24){
			return "Normal";
		}
		else if(Weight / (Height * Height) >= 24 && Weight / (Height * Height) < 28){
			return "Overweight";
		}
		else{
			return "Obese";
		}
	}	
}

题目8

设计一个圆类Circle,包含私有的成员变量radius(半径);公有的构造方法,对radius进行初始化;公有的方法double getArea(),获取圆的面积;公有的方法double getPerimeter(),获取圆的周长。

设计一个圆柱体类Cylinder,继承Circle类,还包含私有的成员变量height(圆柱体的高);公有的方法double getVolume(),获取圆柱体体积。

说明:圆周率π取Math类中的类常量PI,即Math.PI 。

例如:

测试Result
Circle c=new Cylinder(5,8);//radius is 5,height is 8.
System.out.printf(“The circle`s area is:%.2f,perimeter is:%.2f.\n”,c.getArea(),c.getPerimeter());
System.out.printf(“The cylinder`s volume is:%.2f.\n”,((Cylinder)c).getVolume());
The circle`s area is:78.54,perimeter is:31.42.
The cylinder`s volume is:628.32.

答案:

class Circle{
	private double radius;
	public Circle(double r){
		radius = r;
	}
	public double getArea(){
		return Math.PI*radius*radius;
	}
	public double getPerimeter(){
		return 2*Math.PI*radius;
	}
}
class Cylinder extends Circle{
	private double height;
	public Cylinder(double r, double h){
		super(r);
		height = h;
	}
	public double getVolume(){
		return height*getArea();
	}
}

题目9

设计一个矩形类Rectangle,包含私有的数据成员宽度(Width)和高度(Height);公有的方法double getArea()返回矩形的面积,公有的方法double getPerimeter()返回矩形的周长。

例如:

测试Result
Rectangle rect=new Rectangle(8,5);
System.out.printf(“The Area is %.2f,Perimeter is %.2f\n”, rect.getArea(),rect.getPerimeter());
The Area is 40.00,Perimeter is 26.00

答案:

class Rectangle{	
	private double Width;
	private double Height;
	Rectangle(double a, double b){
		Width = a;
		Height = b;
	}
	public double getArea(){
		return Width*Height;
	}
	public double getPerimeter(){
		return 2*(Width + Height);
	}
}

题目10

定义一个抽象父类操作系统(OS),包含保护(protected)权限的String类型数据成员版本号(Edition),和两个抽象方法:设置版本号setEdition(String Edition),获取版本号getEdition()。由操作系统类派生出Windows类,Android类,iOS类。在每个子类中重写父类的抽象方法,输出效果如示例所示。

例如:

测试Result
OS os = new Windows();
os.setEdition(“10.0.1”);
System.out.printf("%s\n",os.getEdition());
os = new Android();
os.setEdition(“11.0.1”);
System.out.printf("%s\n",os.getEdition());
os = new iOS();
os.setEdition(“14.2.0”);
System.out.printf("%s\n",os.getEdition());
Windows:10.0.1
Android:11.0.1
iOS:14.2.0

答案:

abstract class OS{
	protected String Edition;
	abstract void setEdition(String Edition);
	abstract String getEdition();
}
class Windows extends OS{
	void setEdition(String Edition){
		this.Edition = Edition;
	}
	String getEdition(){
		return "Windows:"+Edition;
	}
}
class Android extends OS{
	void setEdition(String Edition){
		this.Edition = Edition;
	}
	String getEdition(){
		return "Android:"+Edition;
	}
}
class iOS extends OS{
	void setEdition(String Edition){
		this.Edition = Edition;
	}
	String getEdition(){
		return "iOS:"+Edition;
	}
}
  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值