博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PTA 面向对象(Java)
阅读量:2055 次
发布时间:2019-04-28

本文共 8333 字,大约阅读时间需要 27 分钟。

文章目录

1.构造方法 (15 分)

请补充以下代码,完成输出要求。

public class Main {
public Main(){
System.out.println("构造方法一被调用了"); } public Main(int x){
this(); System.out.println("构造方法二被调用了"); } public Main(boolean b){
this(1); System.out.println("构造方法三被调用了"); } public static void main(String[] args) {
}}

输入格式:

输出格式:

输出以下三行: 构造方法一被调用了 构造方法二被调用了 构造方法三被调用了

输入样例:

输出样例:

构造方法一被调用了
构造方法二被调用了
构造方法三被调用了

public class Main {
public Main(){
System.out.println("构造方法一被调用了"); } public Main(int x){
this(); System.out.println("构造方法二被调用了"); } public Main(boolean b){
this(1); System.out.println("构造方法三被调用了"); } public static void main(String[] args) {
Main a=new Main(true); }}

2.学生类-构造函数 (15 分)

定义一个有关学生的Student类,内含类成员变量: String name、String sex、int age,所有的变量必须为私有(private)。

1.编写有参构造函数: 能对name,sex,age赋值。

2.覆盖toString函数:

按照格式:类名 [name=, sex=, age=]输出。使用idea自动生成,然后在修改成该输出格式

3.对每个属性生成setter/getter方法

4.main方法中

•输入1行name age sex , 调用上面的有参构造函数新建对象。

输入样例:

tom 15 male

输出样例:

Student [name=‘tom’, sex=‘male’, age=15]

import java.util.Scanner;class Student {
private String name; private String sex; private int age; public Student(String name, String sex, int age) {
this.name = name; this.sex = sex; this.age = age; } public String toString() {
return "Student [name='" + name + "', sex='" + sex + "', age=" + age+"]"; } public String getName() {
return name; } public void setName(String name) {
this.name = name; } public String getSex() {
return sex; } public void setSex(String sex) {
this.sex = sex; } public int getAge() {
return age; } public void setAge(int age) {
this.age = age; }} public class Main{
public static void main(String[] args) {
Scanner sc =new Scanner(System.in); String name=sc.next(); int age=sc.nextInt(); String sex=sc.next(); Student a=new Student(name,sex,age); System.out.println(a.toString()); }}

3.定义类 (10 分)

请补充以下代码,完成输出要求。(注意:需要提交完整代码)

import java.util.Scanner;public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in); int a,b,c,d,e; a = in.nextInt(); b = in.nextInt(); c = in.nextInt(); d = in.nextInt(); e = in.nextInt(); RR rr = new RR(); double dd = rr.fun(a,b,c,d,e); System.out.printf("%.2f",dd); }}class RR{
}

输入格式:

在一行中给出5个不超过1000的正整数。

输出格式:

输出5个整数的平均值,保留小数点后两位。

输入样例:

1 2 3 4 5

输出样例:

3.00

import java.util.Scanner;public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in); int a,b,c,d,e; a = in.nextInt(); b = in.nextInt(); c = in.nextInt(); d = in.nextInt(); e = in.nextInt(); RR rr = new RR(); double dd = rr.fun(a,b,c,d,e); System.out.printf("%.2f",dd); }}class RR{
double fun(int a, int b, int c, int d, int e){
return (a+b+c+d+e)/5; }}

4.计算年龄 (15 分)

定义一个Birthday类,其成员变量有3个整形变量(出生的年月日):year,month,day;提供构造方法对这3个成员变量进行初始化;提供成员变量的get、set方法;成员函数有getAge(),功能是实现计算到2017年12月25日时该Birthday对象的年龄。编写程序测试这个类。

输入格式:

输入出生的年、月、日(注:输入的年月日以换行隔开)

输出格式:

计算得到年龄

输入样例:

在这里给出一组输入。例如:

1995

12
23

输出样例:

在这里给出相应的输出。例如:

age=22

import java.util.Scanner; class  Birthday {
private int year, month, day; public Birthday(int year, int month, int day) {
this.year = year; this.month = month; this.day = day; } public void setYear(int year) {
this.year = year; } public void setMonth(int month) {
this.month = month; } public void setDay(int day) {
this.day = day; } public int getYear() {
return year; } public int getMonth() {
return month; } public int getDay() {
return day; } } public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in); int year=sc.nextInt(); int month=sc.nextInt(); int day=sc.nextInt(); Birthday a = new Birthday(year,month,day); int age = 2017 - a.getYear(); if (a.getMonth() == 12 & a.getDay() >= 25) {
age++; } System.out.println("age=" + age); } }

5.统计商品总价 (15 分)

消费者购买超市5件商品,输入商品名和价格,输出购买的商品信息,并输出应付的总价。

要求:定义Goods类及其成员变量和方法。(1)定义Goods类:成员变量有 name,  price (2)定义Goods类的带两个参数的构造方法。(3)定义Goods类的toString()方法,getPrice()方法。

输入格式:

输入5行数据,每行一个商品信息,包括商品名和价格,以一个空格分隔。

输出格式:

输出商品信息,格式:商品名,价格
最后输出总价,格式:should pay:总价
裁判程序如下:

class Main{
public static void main(String args[]){
Goods ga[] =new Goods[5]; Scanner sc = new Scanner(System.in); for(int i =0;i<5;i++){
ga[i]= new Goods(sc.next(),sc.nextDouble()); } double shouldPay = 0; for(Goods g:ga){
shouldPay += g.getPrice(); System.out.println(g.toString()); } System.out.println("should pay:"+shouldPay); }}

输入样例:

book 5.5
pencil 1.2
pen 8.0
ruler 2.5
eraser 1.0

输出样例:

book,5.5
pencil,1.2
pen,8.0
ruler,2.5
eraser,1.0
should pay:18.2

import java.util.Scanner;class Main{
public static void main(String args[]){
Goods ga[] =new Goods[5]; Scanner sc = new Scanner(System.in); for(int i =0;i<5;i++){
ga[i]= new Goods(sc.next(),sc.nextDouble()); } double shouldPay = 0; for(Goods g:ga){
shouldPay += g.getPrice(); System.out.println(g.toString()); } System.out.println("should pay:"+shouldPay); }}class Goods{
String name; double price; Goods(){
} Goods(String name,double price){
this.name = name; this.price = price; } public double getPrice() {
return price; } @Override public String toString() {
return name + "," + price; }}

6.定义类与创建对象 (15 分)

定义一个类Person,定义name和age属性,定义有参的构造方法对name和age进行初始化。在测试类中创建该类的2个对象,姓名、年龄分别为lili、19和lucy、20,在屏幕打印出2个对象的姓名和年龄。

输入格式:

本题目无输入

输出格式:

在一行中输出一个人的姓名和年龄

输入样例:

在这里给出一组输入。例如:

输出样例:

在这里给出相应的输出。例如:

this person is lili,her age is 19

this person is lucy,her age is 20

class Person{
String name; int age; Person(String n,int a){
name = n; age=a;}}public class Main{
public static void main(String args[]){
Person p1 = new Person("lili",19); Person p2 = new Person("lucy",20); System.out.println("this person is "+p1.name+ ",her age is "+p1.age); System.out.print("this person is "+p2.name+ ",her age is "+p2.age);}}

7.设计一个矩形类Rectangle (15 分)

设计一个名为Rectangle的类表示矩形。这个类包括:

两个名为width和height的double类型数据域,它们分别表示矩形的宽和高。width和height的默认值都为1。

一个用于创建默认矩形的无参构造方法。
一个创建指定width和height值的矩形的构造方法。
一个名为getArea()的方法,返回该矩形的面积。
一个名为getPerimeter()的方法,返回周长。
编写一个测试程序,分别输入两个矩形的高和宽,创建两个Rectangle对象。按照顺序显示每个矩形的宽、高、面积和周长。

输入格式:

输出格式:

每行输出一个矩形的宽、高、面积和周长,中间用空格隔开

输入样例:

在这里给出一组输入。例如:

4 40 160 88

输出样例:

在这里给出相应的输出。例如:

4.0 40.0 160.0 88.0

3.5 35.9 125.64999999999999 78.8

import java.util.Scanner;public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in); double a=sc.nextDouble(); double b=sc.nextDouble(); double c=sc.nextDouble(); double d=sc.nextDouble(); Rectangle r1=new Rectangle(a,b); Rectangle r2=new Rectangle(c,d); double a1= r1.getArea(); double b1= r1.getPerimeter(); double c1= r2.getArea(); double d1= r2.getPerimeter(); System.out.printf("%.1f %.1f %.1f %.1f\n",a,b,a1,b1); System.out.printf("%.1f %.1f %.1f %.1f\n",c,d,c1,d1); }}class Rectangle{
double width=1;double height=1; public Rectangle(){
width=1;height=1; } public Rectangle(double w,double y){
width=w;height=y; }public double getArea(){
return width*height;} public double getPerimeter(){
return (width+height)*2;} }

转载地址:http://ejnlf.baihongyu.com/

你可能感兴趣的文章
HPE 推出容器平台 Ezmeral,向 VMware 与 Red Hat 下战书
查看>>
使用 Prometheus-Operator 监控 Calico
查看>>
如果你不习惯新版的 Github 的 UI 界面,可以试试这款插件
查看>>
容器化囧途——没上容器时好好的?
查看>>
linux内核网络参数tcp_tw_recycle 和 tcp_tw_reuse 你搞清楚了吗?
查看>>
40核CPU+80G内存的云资源终终终终终于免费了!
查看>>
Drone开源持续集成工具——Pipeline篇
查看>>
Kubernetes 源码剖析之 WorkQueue 队列 | 文末送书
查看>>
根据 PID 获取容器所在的 Pod 名称
查看>>
JAVA 面试很难吗?Oracle 工程师教你轻松搞定BAT!
查看>>
弹窗!到底谁是幕后黑手?
查看>>
免费分享一个最完美的英语学习素材 Englishpod
查看>>
Ceph CSI 3.0.0 发布了!
查看>>
2020年了,SparkStreaming 与 Kafka 还是性格不合。。
查看>>
为了解决 Prometheus 大内存问题,我竟然强行将 Prometheus Operator 给肢解了。。
查看>>
容器视角下的网络性能监控
查看>>
最炫酷的 Kubernetes Dashboard:Octant 迎来重大更新!
查看>>
查询亿级数据毫秒级返回!Elasticsearch 是如何做到的?
查看>>
我就感觉到快 —— zsh 和 oh my zsh 冷启动速度优化
查看>>
Prometheus监控神器-Alertmanager篇(二)
查看>>