2021-07-05

**

7-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);
    }
}

**

7-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.*;
class Student{
    private String name;
    private String sex;
    private int age;

    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 in=new Scanner(System.in);
        String name=in.next();
        int age=in.nextInt();
        String sex=in.next();
        Student a=new Student(name,sex,age);
        System.out.println(a.toString());
    }
}

**

7-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.*;
class RR{
    double fun(int a,int b,int c,int d,int e){
        return (a+b+c+d+e)/5;
    }
}
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);
    }
}

**

7-4 计算年龄 (15 分)

**

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

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

计算得到年龄
输入样例:

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

1995
12
23

输出样例:

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

age=22
源代码:

import java.util.*;
class Birthday{
    int year;
    int month;
    int day;
    public Birthday(int year,int month,int day){
        this.year = year;
        this.month = month;
        this.day = day;
    }
    public int getYear(){
        return year;
    }
    public void setYear(int year){
        this.year = year;
    }
    public int getMonth(){
        return month;
    }
    public void setMonth(int month){
        this.month = month;
    }
    public int getDay(){
        return day;
    }
    public void setDay(int day){
        this.day = 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);
    }
}

**

7-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.*;
class Goods{
    String name;
    double price;
    public Goods(String name,double price){
        this.name = name;
        this.price = price;
    }
    public double getPrice(){
        return price;
    }
    public String toString(){
     return name+","+price;
    }
}
public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        Goods ga[] = new Goods[5];
        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);
    }
}

**

7-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
源代码:

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

**

7-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.*;
class Rectangle{
    double width=1;
    double height=1;
    public Rectangle(){
        this.width=1;
        this.height=1;
    }
    public Rectangle(double w,double h){
        this.width=w;
        this.height=h;
    }
    public double getArea(){
        return width*height;
    }
    public double getPerimeter(){
        return (width+height)*2;
    }
}
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 x = new Rectangle(a,b);
        Rectangle y = new Rectangle(c,d);
        double a1 = x.getArea();
        double b1 = x.getPerimeter();
        double c1 = y.getArea();
        double d1 = y.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);
    }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
写出一个类People,并由该类做基类派生出子类Employee和Teacher。其中People类具有name、age两个保护成员变量,分别为String类型和整型,且具有共有的getAge()成员方法,用于返回age变量的值,并编写一个两个参数的构造函数。Employee类具有保护成员String变量employeeNo,Teacher类有String类型的teano和zc变量,并分别为两个子类编写一个无参的构造函数。若使两个子类正常的编译,请在父类中解决该问题。 public class Employee extends People{ protected String employeeNo ; //construct method Employee() { } } package Exercise1; public class People { protected String name ; protected int age ; //construct method People() { } People(String name,int age) { this.name = name ; this.age = age ; } //getAge public int getAge() { return this.age ; } } package Exercise1; public class Teacher extends People{ protected String teano ; protected String zc ; //construct method Teacher() { } } package Exercise1; public class MainTest { public static void main(String[] args) { // TODO Auto-generated method stub Employee em = new Employee(); em.age = 20; em.employeeNo = "1234560"; em.name = "Jim" ; System.out.println("Employee : name :"+em.name+"age:"+em.age+"emplyeeNo:"+em.employeeNo); Teacher tea = new Teacher(); tea.age = 26; tea.name = "Tom" ; tea.teano= "456"; tea.zc = "Go" ; System.out.println("Teacher : name :"+tea.name+"age:"+tea.age+"teano:"+tea.teano+"zc:"+tea.zc); } } 题目:判断101-200之间有多少个素数,并输出所有素数。 public class SuShu { public static void main(String[] args) { int m; int i; int k=0; for(m=1;m<=100;m++) { for(i=2;i<m;i++) { if((m%i)==0) { break; } } if(i==m) { k++; System.out.println(m); } } System.out.println("sushu you"+k); } } 题目:一个5位数,判断它是不是回文数。即12321是回文数,个位与万位相同,十位与千位相同。 import javax.swing.JOptionPane; public class Exer3 { public static void main(String[] args) { String s1 = JOptionPane.showInputDialog(" Input a string (s1>=10000 && s1<100000) :"); String s2 = new StringBuffer(s1).reverse().toString(); if(s1.equals(s2)){ JOptionPane.showMessageDialog(null,"yes"); }else{ JOptionPane.showMessageDialog(null,"no"); } } } 题目:打印出杨辉三角形(要求打印出10行 import java.util.Scanner; public class YHSanJiao { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter a data:"); int n = input.nextInt(); int[][] a = new int[n][n]; for(int i = 0; i<n ; i++){ for(int j = 0; j <=i;j++){ if(j==0||i==j) a[i][j]=1; else a[i][j] = a[i-1][j-1] + a[i-1][j]; System.out.print(a[i][j] + " "); } System.out.println(); } } } Enter a data:10 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 1 6 15 20 15 6 1 1 7 21 35 35 21 7 1 1 8 28 56 70 56 28 8 1 1 9 36 84 126 126 84 36 9 1 GradeDemo类中的checkAnswer方法实现不定项选择判分,具体要求如下: 1) 考生答案和正确答案相同(不考虑选项顺序),得5分。 2) 考生答案不全,得2分。 3) 考生答案中有错误答案,得0分。 部分示例如下: 正确答案 考生答案 得分 ABC ABC/ACB/BAC/CBA/BCA 5 ABC A/B/C/AB/AC/BC/CB/CA/BA 2 ABC D/AD/BD/CD/ABD/BCD/ACD/ABCD 0 请完成checkAnswer方法,根据正确答案和考生答案,返回得分。 String类的相关方法: public char charAt(int index):返回指定索引处的 char 值 public boolean equals(Object anObject):比较此字符串与指定的对象。 public int indexOf(int ch):返回指定字符在此字符串中第一次出现处的索引, 找不到返回-1 public String substring(int beginIndex, int endIndex):返回一个新的字符串,该 子字符串始于指定索引处的字符,一直到此字符串末尾。 public class GradeDemo { public static void main(String[] args) { String correctAnswer = "ACD"; String userAnswer = "AD"; int grade = checkAnswer(correctAnswer,userAnswer); System.out.println("本题的得分是 : " + grade); } public static int checkAnswer(String correctAnswer,String userAnswer){} public static boolean checkString(String correctAnswer,String userAnswer){} } ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// import javax.swing.JOptionPane; public class CheckAnswer { public static void main(String[] args) { String correctAnswer = "ACD"; String userAnswer = JOptionPane.showInputDialog("考生答案为:"); userAnswer = userAnswer.toUpperCase(); int grade = checkAnswer(correctAnswer,userAnswer); System.out.println("本题的得分是: " + grade); } public static int checkAnswer (String correctAnswer,String userAnswer ){ if(correctAnswer.length()==userAnswer.length()){ if(checkString(correctAnswer,userAnswer)) return 5; else return 0; } else if(correctAnswer.length()>userAnswer.length()){ if(checkString(correctAnswer,userAnswer)) return 2; else return 0; } else return 0; } public static boolean checkString (String correctAnswer,String userAnswer ){ boolean m = true; for(int i = 0; i < userAnswer.length(); ++i){ if(correctAnswer.indexOf(userAnswer.charAt(i))==-1){ m= false; break; } } return m ; } }

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值