jmu-PTA Java答案汇总(上)

目录

Java练习第二章

7-21 统计符合条件元素的个数

7-210 sdut-array2-1-矩阵转置(I)

7-211 JAVA-输入输出入门

7-212 sdut-入门-三个整数和、积与平均值

7-213 jmu-java-m01-System.out.printf入门

7-214 jmu-java-m02-循环求和

7-215 编程题:统计符合条件元素的个数

7-216 jmu-java-m02-数组基本操作

7-22 整数数组比较-Java

7-23 求n以内所有能被3和5整除的正整数

7-24 输出所有的水仙花数

7-25 编写一个程序解决爱因斯坦台阶问题

7-26 判断一个数是否为回文数

7-27 编写程序求1000之内的所有完全数。

7-28 编写Java程序,求13-23+33-43+…+973-983+993-1003的值。

7-29 用foreach求数组之和

Java练习第三章

6-31 定义复数类Complex

6-32 比较两个对象是否相等

6-33 利用静态变量实现自动编号

6-34 编写一个单例类

6-35 定义派生类Rectangle

6-36 编写雇员工资支付程序。

6-37 图书和音像租赁

6-38 定义商品类

6-39 实现部门介绍功能需求说明(继承)

6-310 高速公路车辆收费系统(继承)

6-311 模拟控制动物叫的功能(多态)

6-312 使用多态实现图书馆计算罚金功能

7-31 设计一个People类

7-32 定义商品类,封装成员变量,输出对象

7-33 单例类练习


Java练习第二章

7-21 统计符合条件元素的个数

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        //计算能被3整除的奇数和偶数
        int s = 0;
        int d = 0;
        for (int i = 1; i <= n; i++) {
            if(i % 3 == 0) {
                if(i % 2 == 0) {
                    d++;
                }
                else
                    s++;
            }
        }
        System.out.println(s + "," + d);
    }
}

7-210 sdut-array2-1-矩阵转置(I)

import java.util.Scanner;

public class Main{
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int N = scanner.nextInt();
        int[][] arr = new int[N][N];
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                arr[i][j] = scanner.nextInt();
            }
        }
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                System.out.print(arr[j][i]);
                if(j < N -1)
                    System.out.print(" ");
            }
            System.out.println();
        }
    }
}

7-211 JAVA-输入输出入门

import java.io.*;
import java.util.Scanner;
class Main{
   public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int x = scan.nextInt();
        double y = scan.nextDouble();

        System.out.println(x + y);
        System.out.println(Math.sqrt(x + y));
        System.out.print(String.format("%.6f",Math.sqrt(x+y)));
    }
}

7-212 sdut-入门-三个整数和、积与平均值

import java.io.*; 
import java.util.Scanner;

class Main {

	public static void main(String[] args)throws IOException {
        Scanner scan = new Scanner(System.in);
        int a = scan.nextInt();
        int b = scan.nextInt();
        int c = scan.nextInt();

        System.out.print((a + b + c)+" ");
        System.out.print((a * b * c) + " ");
        System.out.print(String.format("%.2f",((a+b+c)/3.0)));
    }
}

7-213 jmu-java-m01-System.out.printf入门

import java.util.Scanner;

public class Main {
 public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        int a = s.nextInt();
        double b = s.nextDouble();
        int x1 = s.nextInt();
        int x2 = s.nextInt();
        int x3 = s.nextInt();

        System.out.println(a+" "
                            +Integer.toOctalString(a)+" "
                            +Integer.toHexString(a));
        System.out.println(String.format("%6.2f", b));
        System.out.println(String.format("%5d",x1)+" "+String.format("%5d",x2)+" "+String.format("%5d",x3));
        System.out.println(String.format("%5d",x2)+" "+String.format("%5d",x3)+" "+String.format("%5d",x1));
        System.out.println(String.format("%5d",x3)+" "+String.format("%5d",x1)+" "+String.format("%5d",x2));

    }
}

7-214 jmu-java-m02-循环求和

import java.io.*;
import java.util.Scanner;

class Main{
    public static void main(String[]args){
        Scanner s = new Scanner(System.in);
        int n = s.nextInt();
        int[] arr = new int[n];
        int sum1 = 0;
        int sum2 = 0;
        for (int i = 0; i < n; i++) {
            arr[i] = s.nextInt();
            if(arr[i] % 2 == 0) {
                sum2+=arr[i];
            }
            else
                sum1+=arr[i];
        }
        System.out.println("奇数和="+sum1+", 偶数和="+sum2);
    }
}

7-215 编程题:统计符合条件元素的个数

import java.io.*; 
import java.util.Scanner;
class Main{
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        int s = 0;
        int d = 0;
        for (int i = 1; i <= n; i++) {
            if(i % 3 == 0) {
                if(i % 2 ==0)
                    d++;
                else
                    s++;
            }
        }
        System.out.println(s + "," + d);
    }
}

7-216 jmu-java-m02-数组基本操作

import java.util.Scanner;
import java.util.Arrays;
public class Main{
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        int[] R = new int[n];
        for (int i = 0; i < n; i++) {
            R[i] = scan.nextInt();
        }
        //排序
        Arrays.sort(R);
        System.out.println(Arrays.toString(R));
        //逆序
        for (int i = R.length-1; i >= 0 ; i--) {
            System.out.print(R[i]+" ");
        }
        System.out.println();
        //平均半径
        double mid= Arrays.stream(R).sum()/(double)n;
        System.out.println("平均半径="+mid);
        //总面积
        double area = 0;
        for (int x:R) {
            area+=x*x*Math.PI;
        }
        System.out.println("总面积="+String.format("%.6f",area));
    }
}

7-22 整数数组比较-Java

import java.util.Arrays;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        int[] A = new int[n];
        for (int i = 0; i < n; i++) {
            A[i] = scan.nextInt();
        }
        int[] B = A.clone();
        Arrays.sort(B);
        int ab = 0;
        int ba = 0;
        int d = 0;
        for (int i = 0; i < n; i++) {
            if(A[i] > B[i]) {
                ab++;
            }
            else if(A[i] < B[i]) {
                ba++;
            }
            else
                d++;

        }
        System.out.println(ab);
        System.out.println(d);
        System.out.println(ba);
    }
}

7-23 求n以内所有能被3和5整除的正整数

import java.io.*;
import java.util.Scanner;

class Main{
    public static void main(String[]args){
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        int key = 0;
        for (int i = 1; i <= n; i++) {
            if(i % 3 == 0 && i % 5 == 0) {
                System.out.print(i+" ");
                key++;
            }
            if(key == 10) {
                System.out.println();
                key -= 10;
            }
        }
    }
}

7-24 输出所有的水仙花数

import java.io.*;


class Main{
    public static void main(String[]args){
        for (int i = 100; i < 1000; i++) {
            int a = i % 10;
            int b = i / 10 % 10;
            int c = i / 100;
            if(a*a*a + b*b*b + c*c*c == i) {
                System.out.println(i);
            }
        }
    }
}

7-25 编写一个程序解决爱因斯坦台阶问题

public class Main{
    public static void main(String[]args){
        int x = 1;
        while(true) {
            if(x % 2 == 1 && x % 3 == 2 && x % 4 ==3
                &&x % 5 == 4 && x % 6 ==5 && x% 7 == 0) {
                System.out.println(x);
                break;
            }
            x++;
        }
    }
}

7-26 判断一个数是否为回文数

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt(); //n是要判断的数
        int num = 1;    //num有几位数
        int key = 1;    //key用于计算
        int tmp = n;
        while(tmp > 10) {
            tmp /= 10;
            num++;
            key *= 10;
        }
        tmp = n;
        while(tmp > 10) {
            int a = tmp / key;
            int b = tmp % 10;
            if(a != b) {
                System.out.println(n + " 不是回文数");
                return;
            }
            tmp%=key;
            tmp/=10;
            key/=100;
        }
        System.out.println(n + " 是回文数");
    }
}

7-27 编写程序求1000之内的所有完全数。

class Main{
	public static void main(String[] args) {
        for (int i = 1; i < 1000; i++) {
            int sum = 0;
            for (int j = 1; j < i; j++) {
                if(i % j == 0) {
                    sum += j;
                }
            }
            if(sum == i) {
                System.out.print(i+" ");
            }
        }
    }
}

7-28 编写Java程序,求13-23+33-43+…+973-983+993-1003的值。

 class Main {
	public static void main(String[] args) {
        int sum = 0;
        int k = 1;
        for (int i = 13; i <= 1003; i+=10) {
            sum += i * k;
            k *= -1;
        }
        System.out.println(sum);
    }
}

7-29 用foreach求数组之和

import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = scan.nextInt();
        }
        int sum = 0;
        for(int x:arr) {
            sum+=x;
        }
        System.out.println(sum);
    }
}

Java练习第三章

6-31 定义复数类Complex

class Complex {
    double a;
    double b;
    public Complex(double a,double b) {
        this.a = a;
        this.b = b;
    }

    public Complex add(Complex c) {
        Complex tmp = new Complex(this.a,this.b);
        tmp.a+=c.a;
        tmp.b+=c.b;
        return tmp;
    }
    public Complex sub(Complex c) {
        Complex tmp = new Complex(this.a,this.b);
        tmp.a-=c.a;
        tmp.b-=c.b;
        return tmp;
    }

    public String getString() {
        return "("+a+","+b+"i)";
    }
}

6-32 比较两个对象是否相等

class Bird {
    String color;
    int size;

    public Bird(String color, int size) {
        this.color = color;
        this.size = size;
    }

    public String equals(Bird b) {
        if(this.color == b.color && this.size == this.size)
            return "同一只鸟";
        return "不同一只鸟";
    }
}

6-33 利用静态变量实现自动编号

class Book {
    String name;
    double price;
    int id;
    public static int num = 0;
    public  Book() {
        num++;
        id=num;
    }
    public Book(String name, double price) {
        this();
        this.name = name;
        this.price = price;
    }
    public String toString() {
        return "编号:" +id+
                ",书名:" + name +
                ",价格:" + price;
    }
}

6-34 编写一个单例类

class SingleTest {
    private static int bucket = 100;
    private static SingleTest test = new SingleTest();
    public static SingleTest getInstance() {
        return test;
    }
    public void addobj() {
        System.out.println("当前的储量是"+(++bucket));
    }
    public void delOjb() {
        System.out.println("当前的储量是"+(--bucket));
    }
}

6-35 定义派生类Rectangle

class Rectangle extends Shape {
    protected double width;
    protected double height;
    public Rectangle(double width, double height) {
        this.width = width;
        this.height = height;
    }
    public Rectangle(int id, String color, double width, double height) {
        super(id, color);
        this.width = width;
        this.height = height;
    }

    @Override
    public void area() {
        System.out.println(width * height);
    }
}

6-36 编写雇员工资支付程序。

class Manager extends Employee{
    private int salary;

    public Manager(String name, String type, int salary) {
        super(name, type);
        this.salary = salary;
    }

    @Override
    public int getSalary() {
        return salary;
    }
}

class Salesman extends Employee {
    private int salary;
    private int salaryRaise;

    public Salesman(String name, String type, int salary, int salaryRaise) {
        super(name, type);
        this.salary = salary;
        this.salaryRaise = salaryRaise;
    }

    @Override
    public int getSalary() {
        return salary+salaryRaise;
    }
}
class Worker extends Employee {
    private int salary;
    private int days;

    public Worker(String name, String type, int salary, int days) {
        
          super(name, type);
        this.salary = salary;
        this.days = days;
    }

    public int getSalary() {
        return salary*days;
    }
}

6-37 图书和音像租赁

class Media {
    double getDailyRent() {
        return 0;
    }
}
class Book extends Media {
    String name;
    double price;
    public Book(String name, double price) {
        this.name = name;
        this.price = price;
    }
    double getDailyRent() {
        return price*0.01;
    }
}
class DVD extends Media {
    String name;

    public DVD(String name) {
        this.name = name;
    }

    double getDailyRent() {
        return 1.0;
    }
}
class MediaShop extends Media {
    static double calculateRent(Media[] medias, int days) {
        double sum = 0;
        for (int i = 0; i < medias.length; i++) {
            sum+=medias[i].getDailyRent()*days;
        }
        return sum;
    }
}

6-38 定义商品类

class Goods {
    private String id;
    private String name;
    private double price;
    public Goods(String id, String name, double price) {
        this.id = id;
        this.name = name;
        this.price = price;
    }

    @Override
    public String toString() {
        return  id + "," + name + "," + price ;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }
}

6-39 实现部门介绍功能需求说明(继承)

class Department {
    private String sector;
    private String name;
    private int num;
    private String work;
    public Department(String sector, String name, int num, String work) {
        this.sector = sector;
        this.name = name;
        this.num = num;
        this.work = work;
    }
    public String printIntro() {
        return "部门名称:"+sector+"\n"
                +"经理:"+name+"\n"
                +"员工人数:"+num+"\n"
                +"部门职责:"+work+"\n";
    }
}

class HR extends Department {
    private int plan;
    public HR(String sector, String name, int num, String work, int plan) {
        super(sector, name, num, work);
        this.plan = plan;
    }

    @Override
    public String printIntro() {
        return super.printIntro()+"招聘目标:"+plan+"人";
    }
}
class RD extends Department {
    private int number;

    public RD(String sector, String name, int num, String work, int number) {
        super(sector, name, num, work);
        this.number = number;
    }
    @Override
    public String printIntro() {
        return super.printIntro()+"研发项目数:"+number+"人";
    }
}

6-310 高速公路车辆收费系统(继承)

class Vehicle {
    int length;
    String plateNo;
    public Vehicle(int length, String plateNo) {
        this.length = length;
        this.plateNo = plateNo;
    }
    public Vehicle(int length) {
        this.length = length;
    }

    public String getString() {
        return "车牌号:"+plateNo+"\n"
                +"车长:"+length+"\n";
    }
    public double getRate() {
        return 0;
    }
}
class Bus extends Vehicle{
    int passengers;
    public Bus(int length, String plateNo, int passengers) {
        super(length, plateNo);
        this.passengers = passengers;
    }
    @Override
    public String getString() {
        return super.getString()+"核定载人数:"+passengers;
    }

    @Override
    public double getRate() {
        if(length < 6000) {
            return 0.6;
        }
        else {
            return 0.9;
        }
    }
}
class Truck extends Vehicle {
    double weight;
    int numbersOfAxles;
    public Truck(int length, String plateNo, double weight, int numbersOfAxles) {
        super(length, plateNo);
        this.weight = weight;
        this.numbersOfAxles = numbersOfAxles;
    }
    @Override
    public String getString() {
        return super.getString()+"最大允许总重量:"+weight
                +"\n"+"车轴数:"+numbersOfAxles;
    }
    @Override
    public double getRate() {
        if(numbersOfAxles == 2) {
            if(length < 6000 && weight < 4500)
                return 0.6;
            else
                return 0.9;
        }
        if (numbersOfAxles == 3)
            return 1.02;
        if(numbersOfAxles == 4)
            return  1.315;
        if(numbersOfAxles == 5 || numbersOfAxles == 6)
            return 1.428;
        else
            return -1;
    }
}

6-311 模拟控制动物叫的功能(多态)

class Animal {
    public void cry() {
        System.out.println("动物叫......");
    }
}
class Dog extends Animal {
    @Override
    public void cry() {
        System.out.println("汪汪汪......");
    }
}
class Cat extends Animal {
    @Override
    public void cry() {
        System.out.println("喵喵喵......");
    }
}
class Duck extends Animal {
    @Override
    public void cry() {
        System.out.println("嘎嘎嘎......");
    }
}
class Host {
    public void letCry(Animal animal) {
        animal.cry();
    }
}

6-312 使用多态实现图书馆计算罚金功能

class Book {
    String name;
    int borrowDays;
    public Book(String name) {
        this.name = name;
    }
    public int calFines(int borrowDays) {
        return 0;
    }

    public String getName() {
        return name;
    }
}
class AdultBook extends Book {
    public AdultBook(String name) {
        super(name);
    }

    @Override
    public int calFines(int borrowDays) {
        int num = borrowDays - 21;
        if(num <= 0)
            return 0;
        if(num <= 3)
            return num * 2;
        else
            return (num - 3) * 5 + 3 * 2;
    }
}
class KidBook extends Book {
    public KidBook(String name) {
        super(name);
    }

    public int calFines(int borrowDays) {
        int num = borrowDays - 21;
        if(num <= 0)
            return 0;
        else
            return num;
    }
}
class Disc extends Book {
    public Disc(String name) {
        super(name);
    }

    public int calFines(int borrowDays) {
        int num = borrowDays - 14;
        if(num <= 0)
            return 0;
        if(num <= 3)
            return num * 5;
        else
            return (num - 3) * 10 + 3 * 5;
    }
}
class Customer {
    public double calTotalFines(Book books[], int borrowDays) {
        int sum = 0;
        for (int i = 0; i < books.length; i++) {
            sum += books[i].calFines(borrowDays);
        }
        return sum;
    }
}

7-31 设计一个People类

public class Main {
    public static void main(String[] args) {
        /********* begin *********/
        // 声明并实例化一Person对象p
        Person p = new Person();
        // 给p中的属性赋值
        p.setName("张三");
        p.setAge(18);
        // 调用Person类中的talk()方法
        p.talk();
        /********* end *********/
    }
}

// 在这里定义Person类
class Person {
    private String name;
    private int age;
    public void talk() {
        System.out.println("我是:"+name+",今年:"+age+"岁");
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

7-32 定义商品类,封装成员变量,输出对象

class Shop {
    private String id;
    private String name;
    private double price;

    public Shop(String id ,String name,double price) {
        this.id=id;
        this.name=name;
        this.price=price;
    }

    public String toString() {
        return id+","+name+","+price;
    }

    public String getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public double getPrice() {
        return price;
    }

    public void setId(String id) {
        this.id = id;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setPrice(double price) {
        this.price = price;
    }
}

public class Main {
    public static void main(String[] args) {
        Shop s = new Shop("WJ002","记事本",5.5);
        System.out.println(s.toString());
    }
}

7-33 单例类练习

class Printer {
    String number;
    double price;
    private static Printer P = new Printer("惠普",3400);
    private Printer(String number,double price) {
        this.price=price;
        this.number=number;
    }
    public static Printer getPrinter() {
        return P;
    }
    public String toString() {
        return "型号为:"+number+",价格为:"+price;
    }
}
public class Main {
    public static void main(String[] args) {
        Printer P = Printer.getPrinter();
        
        System.out.println(P.toString());
    }
}
  • 17
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值