Java-exam

Java

一卷

T1

/*
编写一个Java程序,求1!+2!+…+10!的值,程序文件命名为“FactoriesSum.java”。
 */
package Test.A基础语法.T1;

public class FactoriesSum {
    public static void main(String[] args) {
        int sum = 0,num=1;
        for (int i=1;i<=10;i++){
            num=num*i;
            sum=sum+num;
        }
        System.out.println(sum);
    }
}

T4

/*
编写一个Java程序,输出1949年-2023年内的闰年,要求按行输出,一行5个年份,用空格分隔,程序文
件命名为“LeapYear.java”。
 */

package Test.A基础语法.T4;

public class LeapYear {
    public static void main(String[] args) {

        int count = 0;
        for (int year = 1949; year <= 2023; year++) {
            if (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)) {
                System.out.print(year + " ");
                count++;
                if (count % 5 == 0) {
                    System.out.println();
                }
            }
        }
    }
}

T6

package Test.B面向对象编程.T6;
import java.math.*;
/*
* (1)定义一个形状的抽象类:Shape,包含两个抽象方法:
① getArea:获取形状的面积;
② getPerimeter:获取形状的周长;
(2)再定义两个子类-圆形类和矩形类:Circle和Rectangle,分别继承自Shape类。
① Circle类中需要包含一个私有变量-半径radius和一个构造方法,构造方法带有一个参数
radius,通过构造方法可以设置矩形的半径;并且需要实现getArea和getPerimeter方法分别
获取圆形的面积和周长。
② Rectangle类中需要包含两个私有变量-长度length和宽度width;以及构造方法,构造方法
带有两个参数length和width,通过构造方法可以设置矩形的长和宽;并且需要实现getArea
和getPerimeter方法分别获取矩形的面积和周长。
(3)最后,编写一个测试类(主类)Test,在main方法中创建一个Circle和一个Rectangle对象,并
分别调用它们的getArea和getPerimeter方法,打印它们的面积和周长
* */
abstract class Shape{
    public double getArea(){
        return 0;
    }
    public double getPerimeter(){
        return 0;
    }
}
 class Circle extends Shape{
    private double radius;
    public Circle(double radius){
        this.radius = radius;
    }
    public double getArea(){
        return Math.PI*radius*radius;
    }

    public double getPerimeter(){
        return 2*Math.PI*radius;
    }
}
 class Rectangle extends Shape{
    private double length;
    private double width;
    public Rectangle(double length,double width){
        this.length = length;
        this.width = width;
    }
    public double getArea(){
        return length*width;
    }

    public double getPerimeter(){
        return 2*(length+width);
    }
}
public class test {
    public static void main(String[] args) {
        Circle circle = new Circle(5);
        Rectangle rectangle = new Rectangle(4,6);
        System.out.println("圆的面积为:"+circle.getArea());
        System.out.println("圆的周长为:"+circle.getPerimeter());
        System.out.println("矩形的面积为:"+rectangle.getArea());
        System.out.println("矩形的周长为:"+rectangle.getPerimeter());
    }
}

T7

package Test.B面向对象编程.T7;
/*
编写接口Animal。Animal接口有两个抽象方法cry()和getAnimalName(),按要求,实现该接口的
各种具体动物类给出自己的叫声和种类名称。设计一个动物声音模拟器,希望模拟器可以模拟许多动
物的叫声,要求如下:
(1)编写接口Animal。Animal接口有两个抽象方法cry()和 getAnimalName(),即要求实现该接
口的各种具体动物类给出自己的叫声和种类名称。
(2)编写模拟器类Simulator。该类有一个 playSound(Animal animal)方法,该方法的参数是
Animal类型,即参数animal可以调用实现Animal接口类重写的cry()方法播放具体动物的声音,
调用重写的getAnimalName()方法显示动物种类的名称。
(3)编写实现Animal接口的 Dog类和Cat类。
 */
abstract interface Animal{
    void cry();
    String getAnimalname();
}
class Simulator{
    public void playground(Animal animal){

        System.out.println(animal.getAnimalname());
        animal.cry();
    }
}
class Dog implements Animal{
    public String getAnimalname(){
        return "Dog";
    }
    public void cry(){
        System.out.println("汪汪汪");
    }
}
class Cat implements Animal{
    public String getAnimalname(){
        return "Cat";
    }
    public void cry(){
        System.out.println("喵喵喵");
    }
}
public class T7 {
    public static void main(String[] args) {
        Simulator simulator = new Simulator();
        simulator.playground(new Dog());
        simulator.playground(new Cat());
    }
}



T9

package Test.Java综合应用.T9;

import java.util.Scanner;

/*
编写一个Java程序,命名为“ReverseStr.java”,实现如下功能:
控制台输入一个字符串s,请你反转字符串中单词的顺序。单词是由非空格字符组成的字符串。字符串中
每个单词间使用一个或多个空格隔开,单词数量不超过10个。返回单词顺序颠倒且单词之间用单个空格
连接的结果字符串。
注意:输入字符串 s中可能会存在前导空格、尾随空格。返回的结果字符串中,单词间应当仅用单个空
格分隔,且不包含任何额外的空格。
例如:
输入:I am a student
输出:student a am I
 */
public class ReverseStr {

    public static void main(String[] args) {
        String s ;
         //s = "I am a student";
        Scanner scanner = new Scanner(System.in);
        s = scanner.nextLine();

        String[] words = s.split(" ");
        //String[] words = s.split("\\s+");
        for(int i = words.length-1;i>=0;i--){
            if (i != 0) {
                System.out.print(words[i] + " ");
            } else {
                System.out.print(words[i]);
            }
        }
    }
}

二卷

T2

package Test2.A基础语法.T2;
/*
* 编写应用程序,使用for循环语句计算5+55+555+5555+...的前10项之和,程序文件命名为
“SequenceSum.java”。*/
public class SequenceSum {
    public static void main(String[] args) {
        long sum = 0;
        long term = 0;
        for (int i = 0; i < 10; i++) {
            term=term*10+5;
            sum+= term;
            System.out.println(term);
        }
        System.out.println(sum);
    }
}

T3

package Test2.A基础语法.T3;

/*编写程序,输出满足1+2+3+...+n>10000的最小正整数n,程序文件命名为“IntegerMin.java”。*/
public class IntegerMin {
    public static void main(String[] args) {
        int max = 0;
        int i;
        for (i = 1; i < Integer.MAX_VALUE; i++) {
            max = max + i;
            if (max > 10000)
                break;
        }
        System.out.println("满足条件的最小正整数n为:" + i);
    }
}

T6

package Test2.B核心语法.T6;
/*
* 设计一个计算柱体应用程序,希望该应用程序可以计算不同柱体的体积,要求如下:
(1) 首先设计一个抽象类Geometry(几何图形)类,在该抽象类中定义一个抽象getArea()方法.
(2) 然后设计PillarL(柱体)类,该类的对象调用getVolume()方法计算柱体的体积,
(3) 分别设计Circle(圆)类和Rectangle(矩形)类和Triangle(三角形)类作为Geometry类的子类。
(4) 最后设计Application测试类(主类)分别求出圆柱的体积(底面圆r=10),四棱柱的体积(底面矩形长12,
宽10),三棱柱的体积(底面三角形三边长3,4,5)。
提示:三角形的面积可以由海伦公式计算。
海伦公式:如果一个三角形的三边长为a,b,c,设p=(a+b+c)/2,则三角形的面积:UML图参考*/
 abstract class Geometry {
    public abstract double getArea();

}
class Pillar {
    Geometry bottom; //bottom是抽象类Geometry声明的变量
    double height;
    Pillar (Geometry bottom,double height) {
        this.bottom=bottom; this.height=height;
    }
    public double getVolume() {
        if(bottom==null) {
            System.out.println("没有底,无法计算体积");
            return -1;
        }
        return bottom.getArea()*height; //bottom可以调用子类重写的getArea方法
    }
}
 class Circle extends Geometry {
    double r;
    Circle(double r) {
        this.r=r;
    }
    public double getArea() {
        return(3.14*r*r);
    }
}
 class Rectangle extends Geometry{
    double a,b;
    Rectangle(double a,double b) {
        this.a=a; this.b=b;
    }
    public double getArea() {
        return a*b;
    }
}
class Triangle extends Geometry {
    double a,b,c;
    public Triangle(double a, double b, double c) {
        this.a = a;
        this.b = b;
        this.c = c;
    }
    public double getArea(){
        double p=(a+b+c)/2;
        return Math.sqrt(p*(p-a)*(p-b)*(p-c));
    }
}
public class T6 {
    public static void main(String args[]){
        Pillar pillar;
        Geometry bottom;
        bottom=new Rectangle(12,10);
        pillar =new Pillar (bottom,10); //pillar是具有矩形底的柱体
        System.out.println("四棱柱体积为:"+pillar.getVolume());
        bottom=new Circle(10);
        pillar =new Pillar (bottom,10); //pillar是具有圆形底的柱体
        System.out.println("圆柱体积为:"+pillar.getVolume());
        bottom=new Triangle(3,4,5);
        pillar=new Pillar(bottom,10);
        System.out.println("三棱柱体积为:"+pillar.getVolume());
    }
}

T7

package Test2.B核心语法.T7;
/*
* 设计一个应用程序,该应用程序用接口实现计算三个数的算术平均数和几何平均数,例如有两个数a,b,c
其算术平均数为:(a+b+c)/3;其几何平均数为: 。要求如下:
(1)设计一个接口(CompurerAverage),该接口有一个抽象方法average(),要求实现该接口的
类完成平均数的计算。
(2)设计一个ArithmeticAverage类实现算术平均数的运算。
(3)设计一个GeometryAverage类实现几何平均数的运算。
(4)设计一个TestAverage测试类(主类)完成测试,a、b、c的值分别为11、12、15。*/
interface ComputerAverage {
    public double average(double a,double b,double c);
}
class ArithmeticAverage implements ComputerAverage{
    public double average(double a, double b, double c) {
        double aver=0;
        aver=(a+b+c)/3;
        return aver;
    }
}
class GeometryAverage implements ComputerAverage{
    public double average(double a,double b,double c) {
        double aver=0;
        aver=Math.pow((a*b*c),1.0/3);
        return aver;
    }
}
public class T7 {
    public static void main(String args[]) {
        ComputerAverage computer;
        double a=11,b=12,c=15;
        computer = new ArithmeticAverage();
        double result = computer.average(a,b,c);
        System.out.printf("%5.2f,%5.2f和%5.2f的算术平均值:%5.2f\n",a,b,c,result);
        computer = new GeometryAverage();
        result= computer.average(a,b,c);
        System.out.printf("%5.2f,%5.2f和%5.2f的几何平均值:%5.2f",a,b,c,result);
    }
}

T8

package Test2.C综合应用.T8;
import java.util.InputMismatchException;
import java.util.Scanner;
/*
* 使用Scanner类的实例解析字符串“欢迎光临美好生活超市,你所买的苹果20.5元,梨20元,铅笔5.8元,香
蕉30.5元,矿泉水20.8元。”中的金额,并计算出此次在超市中花费的总额。要求如下:
(1)设计一个givePriceSum类用于解析字符串中的金额。
(2)设计一个PriceSumTest测试类测试结果。*/
 class GetPrice {
    public static double givePriceSum(String cost) {
        Scanner scanner = new Scanner(cost);
        scanner.useDelimiter("[^0123456789.]+"); //scanner设置分隔标记
        double sum=0;
        while(scanner.hasNext()){
            try{ double price = scanner.nextDouble();
                sum = sum+price;
            } catch(InputMismatchException exp){
                String t = scanner.next();
            }
        } return sum;

}
public static class PriceSumTest {
    public static void main(String args[]) {
        String cost = "欢迎光临美好生活超市,你所买的苹果20.5元,梨20元,铅笔5.8元,香蕉 30.5元,矿泉水20.8元。";
        double priceSum = GetPrice.givePriceSum(cost);
        System.out.printf("%s\n你此次花费的金额为:%.2f元\n",cost,priceSum);
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值