pta面向对象习题答案1

1.定义Student类

(1)成员变量有:姓名,年龄。
(2)对成员变量进行封装。
(3)定义getXXXX,setXXXX方法,其中对年龄的限定条件是:年龄大于0。

定义主类,包含主方法

实现输入5个学生,输出年龄不符合要求 的学生人数和姓名。
如果年龄全部正确,输出“right”,如果全部错误,输出"all wrong"。

输入格式:

5行,每行1个学生信息,包括姓名和年龄

输出格式:

多行,第1行是不符合要求的人数
其余各行是不符合要求的学生的姓名
如果年龄全部正确,输出“right”,如果全部错误,输出"all wrong"。

输入样例:

zhang 18
Li -15
wang 0
zhao 20
wu -20

输出样例:

3
Li
wang
wu

代码长度限制

16 KB

时间限制

400 ms

内存限制

64 MB

答案:

import java.util.Scanner;

class Student{
    public static int t=0;
    String name;
    int age;
    public Student(String name,int age){
        this.name=name;
        this.age=age;
    }
     public String Getname(){
        return name;
    }
    public void Setname(String name)
    {
        this.name=name;
    }
    public int Getage(){
        return age;
    }
    public  void Setage(int age){
        if (age<=0)
        {
            t++;
        }
        else    
            this.age=age;
    }

}
public class Main {
    public static void main(String[] args) {
        Scanner s=new Scanner(System.in);
        String y1[]=new String[5];
        int y2[]=new int[5];
        for (int i=0;i<5;i++){
        String m=s.next();
        y1[i]=m;
        int n=s.nextInt();
        y2[i]=n;
        Student a=new Student(m,n);
        a.Getname();
        a.Setname(m);
        a.Getage();
        a.Setage(n);
        }
        if (Student.t==0)
        {System.out.println("right");
        System.exit(0);}
       if (Student.t==5)
       {System.out.println("all wrong");
       System.exit(0);}
       System.out.println(Student.t);
        for (int i=0;i<5;i++)
        {
            if (y2[i]<=0)
                System.out.println(y1[i]);
        }
    }

}

2.7-38 通过键盘输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数

统计一行字符串中的英文字母个数、空格个数、数字个数、其他字符个数

输入格式:

通过键盘输入一行字符(任意字符)

输出格式:

统计一行字符串中的英文字母个数、空格个数、数字个数、其他字符个数

输入样例:

rwrwewre2345asdJSJQI%^&(&   *&sdf YY( 2342-k'

输出样例:

字母个数:22
数字个数:8
空格个数:5
其他字符个数:10

代码长度限制

16 KB

时间限制

400 ms

内存限制

64 MB

答案:

import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner sc= new Scanner(System.in);
		String a=sc.nextLine();
		sc.close();
		char array[]=new char[100];
		for(int i=0;i<a.length();i++)
		{
			array[i]=a.charAt(i);
		}
		int number=0;
		int alfer=0;
		int blank=0;
		int others=0;
		for(int j=0;j<a.length();j++)
		{
			if(array[j]>='0'&&array[j]<='9')
			{
				number++;
			}
			else if(array[j]==' ')
			{
				blank++;
			}
			else if((array[j]>='a'&&array[j]<='z')||(array[j]>='A'&&array[j]<='Z'))
			{
				alfer++;
			}
			else
			{
				others++;
			}
		}
		System.out.println("字母个数:"+alfer);
		System.out.println("数字个数:"+number);
		System.out.println("空格个数:"+blank);
		System.out.println("其他字符个数:"+others);
  }
}

3.7-74 Person类-静态变量

在Person类的基础上,添加一个静态变量avgAge表示所有Person对象的平均年龄(整数),提供方法getAvgAge能够读取该静态变量。
main函数中,构造三个Person类的对象,读入他们的信息,并输出他们的平均年龄

输入格式:

多个用户信息

输出格式:

平均年龄

输入样例:

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

a male 23
b female 21
c male 22

输出样例:

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

22

代码长度限制

16 KB

时间限制

400 ms

内存限制

64 MB

答案:

import java.util.Scanner;
public class Main {
	public static void main(String[] args)  {
		// TODO 自动生成的方法存根
		    Scanner sc=new Scanner(System.in);
		    String name1=sc.next();
		    String sex1=sc.next();
		    int age1=sc.nextInt();
		    String name2=sc.next();
		    String sex2=sc.next();
		    int age2=sc.nextInt();
		    String name3=sc.next();
		    String sex3=sc.next();
		    int age3=sc.nextInt();
		    sc.close();
		    Person p1=new Person(name1,sex1,age1);
		    Person p2=new Person(name2,sex2,age2);
		    Person p3=new Person(name3,sex3,age3);
		    int average=(age1+age2+age3)/3;
		    System.out.println(average);
		  }
		}
		
class Person
{
    String name;
    String sex;
    int age;
    Person(String name,String sex,int age)
    {
        this.name=name;
        this.sex=sex;
        this.age=age;
    }
}

4.7-101 圆柱体类设计

    1. 定义一个圆柱类Cylinder
    2. 里面包含私有属性 private int radius(半径),height(高)
    3. 为属性完成其setter getter方法
    4. 完成带参构造方法Cylinder(int radius,height),该方法中包含一句System.out.println("Constructor with para");
    5. 完成无参构造方法Cylinder(),在无参构造方法中调用有参构造方法,为半径和高赋值为2,1,该方法包含一句System.out.println("Constructor no para");
    6. 完成求体积方法 public int getVolumn(){} 求圆柱体积,π使用Math.PI
  • 定义测试类Main,在main方法中,按照顺序要求完成下列操作
  • 从键盘接收两个数,第一个为半径,第二个为高,并利用刚才输出两个数创建圆柱体对象c1,求c1的体积并输出。
  • 使用无参构造方法 创建第二个圆柱体对象c2,求c2的体积并输出。

输入格式:

在一行中输入半径 和高。

输出格式:

对每一个圆柱体输出它的体积

输入样例:

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

2   3

输出样例:

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

Constructor with para
37
Constructor with para
Constructor no para
12

代码长度限制

16 KB

时间限制

400 ms

内存限制

64 MB

答案:

import java.util.Scanner;
public class Main {
	public static void main(String[] args)  {
		// TODO 自动生成的方法存根
		Scanner sc=new Scanner(System.in);
		int radius=sc.nextInt();
		int height=sc.nextInt();
		Cylinder c1=new Cylinder(radius,height);
		int v=c1.getVolumn();
		System.out.println(v);
        System.out.println("Constructor with para");
		Cylinder c2=new Cylinder();
		int v2=c2.getVolumn();
		System.out.println(v2);
	    }
}

class Cylinder{
    private int radius;
    private int height;
    Cylinder(int radius,int height){
        this.radius=radius;
        this.height=height;
        System.out.println("Constructor with para");
    }
    Cylinder(){
        this.radius=2;
        this.height=1;
        System.out.println("Constructor no para");
    }
    public int getVolumn(){
        int v=(int) (height*radius*radius*Math.PI);
        return v;
    } 
}

5.7-127 校园角色类设计

学校需要构建综合系统,使用者包含多种角色。角色Role分两类:学生Student和雇员Employee;雇员又分为教员Faculty和职员Staff。
每个角色都有姓名、性别。学生有学号、班级。一个雇员有工号、入职日期。教员有职称。职员有职位称号。
请以如下Main类为基础,构建各个角色类,将代码补充完整。

public class Main {
    public static void main(String[] args) {
        Faculty fac = new Faculty("张三",32,"33006","2019","10","25","讲师");
        Student stu=new Student("李四",19,"20201103","202011");
        Staff sta = new Staff("王五",27,"32011","2015","06","17","教务员");        
        fac.show();
        sta.show();
        stu.show();
    }
}

输入格式:

输出格式:

我是张三,年龄32岁。工号是33006,2019年10月25日入职。是一名教师,讲师职称。
我是王五,年龄27岁。工号是32011,2015年6月17日入职。是一名教务员。
我是李四,年龄19岁。学号是20201103,来自202011班。

输入样例:

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


输出样例:

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

我是张三,年龄32岁。工号是33006,2019年10月25日入职。是一名教师,讲师职称。
我是王五,年龄27岁。工号是32011,2015年6月17日入职。是一名教务员。
我是李四,年龄19岁。学号是20201103,来自202011班。

代码长度限制

16 KB

时间限制

400 ms

内存限制

64 MB

答案:

public class Main {
	public static void main(String[] args)  {
		// TODO 自动生成的方法存根
		        Faculty fac = new Faculty("张三",32,"33006","2019","10","25","讲师");
		        Student stu=new Student("李四",19,"20201103","202011");
		        Staff sta = new Staff("王五",27,"32011","2015","6","17","教务员");        
		        fac.show();
		        sta.show();
		        stu.show();
		    }
		}


class Faculty{
	String name;
	int age;
	String workNumber;
	String workYear;
	String workMonth;
	String workDay;
	String position;
	Faculty(String name,int age,String workNumber,String workYear,String workMonth,String workDay,String position){
		this.name=name;
		this.age=age;
		this.workNumber=workNumber;
		this.workYear=workYear;
		this.workMonth=workMonth;
		this.workDay=workDay;
		this.position=position;
	}
	void show()
	{
		System.out.println("我是"+name+",年龄"+age+"岁。工号是"+workNumber+","+workYear+"年"+workMonth+"月"+workDay+"日入职。是一名教师,"+position+"职称。");
	}
}

class Staff{
	String name;
	int age;
	String workNumber;
	String workYear;
	String workMonth;
	String workDay;
	String workName;
	String position;
	Staff(String name,int age,String workNumber,String workYear,String workMonth,String workDay,String workName){
		this.name=name;
		this.age=age;
		this.workNumber=workNumber;
		this.workYear=workYear;
		this.workMonth=workMonth;
		this.workDay=workDay;
		this.workName=workName;
	}
	void show()
	{
		System.out.println("我是"+name+",年龄"+age+"岁。工号是"+workNumber+","+workYear+"年"+workMonth+"月"+workDay+"日入职。是一名"+workName+"。");
	}
}

class Student{
	String name;
	int age;
	String number;
	String iclass;
	Student(String name,int age,String number,String iclass)
	{
		this.name=name;
		this.age=age;
		this.number=number;
		this.iclass=iclass;
	}
	void show() {
		System.out.println("我是"+name+",年龄"+age+"岁。学号是"+number+",来自"+iclass+"班。");
	}
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值