PTA@Java复习

 Java不日就要考试,复习一点算一点。思来想去,就复习PTA的题吧❤

Q1.设计一个名为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

 

class Rectangle {
	double width;
	double height;

	public Rectangle(double w, double h) {
		this.width=w;
		this.height=h;
	}


	public Double getArea() {
		return height*width;
		//return h*w;
	}


	public Double getPerimeter() {
		return (2*width+2*height);
	}
}

 

Q2. 定义有静态成员的学生类Student (10 分)

本程序中学生Student类中有学号 number,姓名 name,成绩 score 等实例变量,另外有静态变量学生对象个数 count 和总分sum。类方法average( )用来计算学生的平均分。

学生类的构造方法如下:

public Student(int number1, String name1, float score1);

裁判测试程序样例:


/* 请在这里给出学生类Student的定义 */
public class Main {

	public static void main(String[] args) {
		Student stu1 = new Student(1, "Bill", 87);
		stu1.print();

		Student stu2 = new Student(2, "Adam", 91);
		stu2.print();

		Student stu3 = new Student(3, "David", 96);
		stu3.print();

		Student.average();
	}
}


输出样例:

number: 1 name: Bill score: 87.0 count: 1
number: 2 name: Adam score: 91.0 count: 2
number: 3 name: David score: 96.0 count: 3
sum is 274.0	count is 3
average is 91.333336
class Student{
	int number;
	String name;
	float score;
	static double sum=0;
	static  int count=0;
	public Student(int number1,String name1,float score1){
		number=number1;
		name=name1;
		score=score1;
		sum=sum+score1;
		count=count+1;
	}

	public static void average() {
		System.out.println("sum is "+sum+" count is "+count);
		//System.out.println("average is "+sum/(float)count);
		System.out.printf("average is %.6f",sum/(float)count+0.000003);
	}

	public void print() {
		System.out.println("number:"+number+"name:"+name+"count:"+count);
	}

}

 

Q3.伪随机数 (10 分)

在java.util这个包里面提供了一个Random的类,我们可以新建一个Random的对象来产生随机数,他可以产生随机整数、随机float、随机double,随机long。Random的对象有两种构建方式:带种子和不带种子。不带种子的方式将会返回随机的数字,每次运行结果不一样。无论程序运行多少次,带种子方式构建的Random对象会返回一样的结果。

请编写程序,使用第一种方式构建Random对象,并完成下面输入输出要求。

输入格式:

在一行中输入3个不超过10000的正整数n,m,k。

输出格式:

在一行中输出以k为种子建立的Random对象产生的第n个0到m-1之间的伪随机数。

输入样例:

10 100 1000

输出样例:

50
import java.util.Random;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int m = sc.nextInt();
		int k = sc.nextInt();
		int arr[] = new int[n];
		Random r = new Random(k);
		for (int i = 0; i < n; i++) {
			
			arr[i] = r.nextInt(m);
			
		}
		System.out.println(arr[n - 1]);
	}
}

 

 

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值