沈师PTA2021Java函数题复习题库

6 篇文章 0 订阅

致谢:感谢@WalkingWithTheWind~帮我寻找PTA中的CSS选择器

1. 数组工具类的设计 (5 分)

本题要求设计一个名为MyArrays的类,根据调用的方式实现相应的方法。
函数接口定义:
请同学根据该类的调用方式和结果,自行设计MyArrays类中的方法,满足应用的需要。
裁判测试程序样例:
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {

        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();

        int array[]= new int[n];

        for(int i=0;i<n;i++)
        {
            array[i]=sc.nextInt();
        }

        MyArrays.printArray(array);//显示数组的内容
        MyArrays.sortArray(array);    //对数组元素排序
        MyArrays.printArray(array);//显示排序后的结果
        int sum=MyArrays.sumOfArray(array);//数组元素求和
        System.out.println(sum);//显示数组元素的和

    }
}

/* 请在这里填写MyArrays类的有关代码 */
输入样例:
5
5 4 6 8 3
结尾无空行
输出样例:
5,4,6,8,3
3,4,5,6,8
26
结尾无空行
答案:

class MyArrays {
    public static void printArray(int[] array){
        for(int i=0;i<array.length-1;i++)
            System.out.print(array[i]+",");
        System.out.println(array[array.length-1]);
    }

    public static void sortArray(int[] array){
        for(int i=0;i<array.length;i++){
            for(int j=i+1;j<array.length;j++){
                if(array[i]>array[j]){
                    int t=array[j];
                    array[j]=array[i];
                    array[i]=t;
                }
            }
        }
    }

    public static int sumOfArray(int[] array){
        int sum=0;
        for(int i=0;i<array.length;i++){
            sum+=array[i];
        }
        return sum;
    }
}

2. 根据派生类写出基类(Java) (5 分)

裁判测试程序样例中展示的是一段定义基类People、派生类Student以及测试两个类的相关Java代码,其中缺失了部分代码,请补充完整,以保证测试程序正常运行。
函数接口定义:
提示:
观察派生类代码和main方法中的测试代码,补全缺失的代码。
裁判测试程序样例:
注意:真正的测试程序中使用的数据可能与样例测试程序中不同,但仅按照样例中的格式调用相关方法(函数)。
class People{
protected String id;
protected String name;

/** 你提交的代码将被嵌在这里(替换此行) **/

}

class Student extends People{
protected String sid;
protected int score;
public Student() {
name = “Pintia Student”;
}
public Student(String id, String name, String sid, int score) {
super(id, name);
this.sid = sid;
this.score = score;
}
public void say() {
System.out.println("I’m a student. My name is " + this.name + “.”);
}

}
public class Main {
public static void main(String[] args) {
Student zs = new Student();
zs.setId(“370211X”);
zs.setName(“Zhang San”);
zs.say();
System.out.println(zs.getId() + " , " + zs.getName());

    Student ls = new Student("330106","Li Si","20183001007",98);
    ls.say();
    System.out.println(ls.getId() + " : " + ls.getName());

    People ww = new Student();
    ww.setName("Wang Wu");
    ww.say();

    People zl = new People("370202", "Zhao Liu");
    zl.say();
}

}
输入样例:
在这里给出一组输入。例如:
(无)
结尾无空行
输出样例:
在这里给出相应的输出。例如:
I’m a student. My name is Zhang San.
370211X , Zhang San
I’m a student. My name is Li Si.
330106 : Li Si
I’m a student. My name is Wang Wu.
I’m a person! My name is Zhao Liu.
结尾无空行
答案:

//无参
	public People() {
		
	}
	//有参
	public People(String id,String name) {
		this.id = id;
		this.name = name;
	}
	//setter & getter
	public void setId(String id) {
		this.id = id;
	}
	
	public String getId() {
		return this.id;
	}
	
	public void setName(String name) {
		this.name = name;
	}
	
	public String getName() {
		return this.name ;
	}
	
	public void say() {
		System.out.println("I'm a person! My name is " + this.name +".");
	}
	

3. 使用继承设计:教师类。 (5 分)

使用继承设计:教师类。 使程序运行结果为:
Li 40 信工院
教师的工作是教学。
函数接口定义:
定义类Teacher, 继承Person
裁判测试程序样例:
class Person{
String name;
int age;
Person(String name,int age){
this.name = name;
this.age = age;
}
void work(){

}
void show() {
    System.out.print(name+" "+age+" ");
}

}

/* 请在这里填写答案 */

public class Main {

public static void main(String[] args) {

     Teacher t = new Teacher("Li",40,"信工院");
     t.show();
     t.work();
}

}
输入样例:
没有输入
输出样例:
Li 40 信工院
教师的工作是教学。
结尾无空行
答案:

class Teacher extends Person{
    String professional;
    Teacher(String name, int age, String professional) {
        super(name, age);
        this.professional=professional;
    }
    @Override
    void work(){
        System.out.println(professional);
        System.out.println("教师的工作是教学。");
    }
}

4. jmu-Java-03面向对象基础-覆盖与toString (5 分)

有Person类,Company类,Employee类。
其中Employee类继承自Person类,属性为:
private Company company;
private double salary;
现在要求编写Employee类的toString方法,返回的字符串格式为:父类的toString-company的toString-salary
函数接口定义:
public String toString()
输入样例:
输出样例:
Li-35-true-MicroSoft-60000.0
答案:

public String toString() {
		return super.toString()+"-"+company.toString()+"-"+salary;
}

5. 重写父类方法equals (5 分)

在类Student中重写Object类的equals方法。使Student对象学号(id)相同时判定为同一对象。
函数接口定义:
在类Student中重写Object类的equals方法。使Student对象学号(id)相同时判定为同一对象。
裁判测试程序样例:
import java.util.Scanner;
class Student {
int id;
String name;
int age;
public Student(int id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}

 /* 请在这里填写答案 */

}
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Student s1 = new Student(sc.nextInt(),sc.next(),sc.nextInt());
Student s2 = new Student(sc.nextInt(),sc.next(),sc.nextInt());
System.out.println(s1.equals(s2));
sc.close();
}
}
输入样例:
1001 Peter 20
1001 Jack 18
结尾无空行
输出样例:
true
结尾无空行
答案:

public boolean equals(Student stu) {
 return stu.id==this.id?true:false;
}

6. 从抽象类shape类扩展出一个圆形类Circle (5 分)

请从下列的抽象类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®;

    System.out.println(d.format(c.getArea()));
    System.out.println(d.format(c.getPerimeter()));
    input.close();
} 

}
输入样例:
3.1415926
结尾无空行
输出样例:
31.0063
19.7392
结尾无空行
答案:

class Circle extends shape
{
	double radius;
	final static double PI = 3.1415926535;
	public Circle(double radius) {
		this.radius = radius;
	}
	@Override
	public double getArea() {
		// TODO Auto-generated method stub
		return PI*radius*radius;
	}
	@Override
	public double getPerimeter() {
		// TODO Auto-generated method stub
		return 2*PI*radius;
	}
	
}

7. 创建一个直角三角形类实现IShape接口 (5 分)

创建一个直角三角形类(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
结尾无空行
答案:

class RTriangle implements IShape{
	double a,b;
	
	public RTriangle(double a, double b) {
		super();
		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);
	}
}

8. jmu-Java-06异常-finally (5 分)

代码中向系统申请资源,到最后都要将资源释放。
现有一Resource类代表资源类,包含方法:
open(String str)打开资源,声明为抛出Exception(包含出错信息)。
close()方法释放资源,声明为抛出RuntimeException(包含出错信息)
现在根据open(String str)中str的不同,打印不同的信息。str的内容分为4种情况:
fail fail,代表open和close均会出现异常。打印open的出错信息与close的出错信息。
fail success,代表open抛出异常,打印open出错信息。close正常执行,打印resource release success
success fail ,代表open正常执行,打印resource open success。close抛出异常,打印close出错信息。
success success,代表open正常执行,打印resource open success,close正常执行打印resource release success。
注1:你不用编写打印出错信息的代码。
注2:捕获异常后使用System.out.println(e)输出异常信息,e是所产生的异常。
裁判测试程序:
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Resource resource = null;
try{
resource = new Resource();
resource.open(sc.nextLine());
/这里放置你的答案/

   sc.close();

}
以下输入样例代表输入success success。
输入样例
success success
输出样例
resource open success
resource release success
答案:

	System.out.println("resource open success");
}
catch(Exception e)
{ 
   	System.out.println(e);
}
try{
    	resource.close();
    	System.out.println("resource release success");
}
catch(RuntimeException e)
{
 	System.out.println(e);
}


9. jmu-Java-06异常-多种类型异常的捕获 (5 分)

如果try块中的代码有可能抛出多种异常,且这些异常之间可能存在继承关系,那么在捕获异常的时候需要注意捕获顺序。
补全下列代码,使得程序正常运行。
裁判测试程序:
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
String choice = sc.next();
try {
if (choice.equals(“number”))
throw new NumberFormatException();
else if (choice.equals(“illegal”)) {
throw new IllegalArgumentException();
} else if (choice.equals(“except”)) {
throw new Exception();
} else
break;
}
/这里放置你的答案/
}//end while
sc.close();
}
###输出说明
在catch块中要输出两行信息:
第1行:要输出自定义信息。如下面输出样例的number format exception
第2行:使用System.out.println(e)输出异常信息,e是所产生的异常。
输入样例
number illegal except quit
结尾无空行
输出样例
number format exception
java.lang.NumberFormatException
illegal argument exception
java.lang.IllegalArgumentException
other exception
java.lang.Exception
结尾无空行
答案:

catch(NumberFormatException e) {
    System.out.println("number format exception");
    System.out.println(e);
}
catch(IllegalArgumentException e) {
    System.out.println("illegal argument exception");
    System.out.println(e);
}
catch(Exception e) {
    System.out.println("other exception");
    System.out.println(e);
}

共计9

进程已结束,退出代码为 0

实验一: Java编程基础 (1) 配置环境变量,熟悉编程环境。 (2) 建立一个Java的Application程序,编译、运行以下例: public class ex01 { public static void main( String arg[ ]) { System.out.println(“hello!”); } } 实验二:流程控制 (1) 编程输出100以内的奇数。 (2) 编程输出乘法表。 (3) 编写程序,定义一个一维数组并赋有初值,同时找出一维数组中的最大值和最小值并输出。 实验三:和对象 (1) 设计一个User,其中包括用户名、口令等属性以及构造方法(至少重载2个)、获取和设置口令的方法、显示和修改用户名的方法等。编写应用程序测试User。 (2) 定义一个Student,其中包括学号、姓名、性别、出生年月等属性以及init( )——初始化各属性、display( )——显示各属性、modify( )¬——修改姓名等方法。实现并测试这个。 (3) 从上的Student中派生出Graduate(研究生),添加属性:专业subject、导师adviser。重载相应的成员方法。并测试这个。 实验四:继承 (1) 定义一个Animal,其中包括昵称、性别、体重属性,构造函数初始化各属性,显示各属性的成员函数、修改属性的成员函数。实现并测试这个。 (2) 从上中派生出Dog,添加年龄属性。重载相应的成员方法,并添加新的方法bark(),输出“wangwangwang”。并测试这个。 实验五:接口 (1) 定义一个接口Inf,含有常量π和一个实现计算功能的方法calculate( ),再分别定义一个面积area和一个周长circumference,各自按计算圆面积和圆周长具体实现接口中的方法,并以半径为5来测试这两个。 (2) 定义一个接口run(),汽车和卡车分别实现这个,汽车实现这个接口输出的是“汽车在跑”,卡车输出的是“卡车在跑”,丰富这两个,在主程序中测试。 实验六:异常处理 (1) 定义一个,在main方法的try块中产生并抛出一个异常,在catch块中捕获异常,并输出相应信息,同时加入finally子句,输出信息,证明它的无条件执行。 (2) *定义一个Caculate实现10以内的整数加减法的计算。自定义一个异常NumberRangeException,当试图进行超范围运算时,产生相应的信息。编写应用程序进行测试。 实验七:图形界面编程 (1) 在窗体上产生一个单文本框和两个命令按纽:“显示”和“清除”。当用户单击“显示”按纽时,在文本框中显示“Java 程序”字样;单击“清除”按纽时清空文本框。 (2)设计如下界面: 当用户输入了两个操作数并点击运算种按纽后,在运算结果对应的文本框中显示运算结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小飞睡不醒

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值