美文网首页
Java第一学期上机试题

Java第一学期上机试题

作者: 秋一哲 | 来源:发表于2019-12-16 17:00 被阅读0次

只供参考 看完请点赞 一个赞就是1/10分钱
1、要求从键盘输入3个数,然后输出其中最大的数!

int a=1,b=2,c=3,x;
x = Math.max(a, b);
x = Math.max(x, c);

2\3 4不写


结果

5.编写一个简单的加密处理程序,从键盘输入一段英文文字,将其中每个字母用其后的第4个字母代替,最后的字母轮回道前面的字符,其他符号不变。如:a用代替;z用d代替。

package Word;
import java.util.Scanner;
public class Work {
    public static void main(String args[]) {
        Scanner io = new Scanner(System.in); //定义输入工具
        String t = io.next(); //输入文本保存到 t
//      t = "Chzx!"; //测试内容 无用
        int n = 4; //表示进4位
        for (int i = 0; i < t.length(); i++) {
            char oneChar = t.charAt(i);//oneChar:一个字符    从t当中挨个取得 
            if (oneChar >= 'a' && oneChar <= 'z' - n || oneChar >= 'A' && oneChar <= 'Z' - n) {
                oneChar += n;
            }else if(oneChar >= 'z'-n && oneChar <= 'z'|| oneChar >= 'Z'-n && oneChar <= 'Z'){
                oneChar -= 26-n;
            }
            System.out.print(oneChar);
        }
    }
}

6、从键盘输入5个任意字符串存到数组中,将数组元素按由小到大排列 。

package Word;
import java.util.Scanner;
public class Work {
    public static void main(String args[]) {
        Scanner io = new Scanner(System.in);
        String[] list = new String[5];
        for (int i = 0; i < 5; i++) {
            list[i] = io.next();
        }
        java.util.Arrays.sort(list); // 排序
        for (int i = 0; i < 5; i++) { // 这里为输出可以不写
            System.out.println(list[i]);
        }
    }
}

7、从键盘输入一段英文文字,然后,将该句反向输出。如:who are you变成uoy era ohw

package Word;
import java.util.Scanner;
public class Work {
    public static void main(String args[]) {
        Scanner io = new Scanner(System.in);
        StringBuffer t = new StringBuffer(io.nextLine());
        t.reverse();
        System.out.print(t);
    }
}

8、String s = "Nice to meet you, Jack!"统计该英文中一共有多少个单词。

package Word;
import java.util.Scanner;
public class Work {
    public static void main(String args[]) {
        String  s = "Nice to meet you, Jack!";
        int n = s.split(" ").length;
        System.out.print(n);
    }
}

9.定义一个 Person类,包括:私有属性姓名、性别、年龄,(可提供对以上私有属性的set与get操作方法。)再定义一个Students类,继承Person类,增加私有属性学号、班级。显示“张三”,“男” ,

package Word;

import java.util.Scanner;
class Person {
    private String name, gender, age;

    public Person(String name, String gender, String age) {
        this.name = name;
        this.gender = gender;
        this.age = age;
    }

    public String getName() {
        return name;
    }

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

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getAge() {
        return age;
    }

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

class Students extends Person {
    private String id, myClass;

    public Students(String name, String gender, String age, String id,
            String myClass) {
        super(name, gender, age);
        this.id = id;
        this.myClass = myClass;
    }

    public String toString() {
        return super.getName() + "," + super.getGender() + "," + super.getAge()
                + "," + this.id + "," + this.myClass;
    }
}

public class Work {
    public static void main(String args[]) {
        Students zhangSan = new Students("张三", "男", "19", "17510101", "175101");
        System.out.print(zhangSan);
    }
}

10、设计一个类,其功能是完成1-n的求和。类中包含一个属性(int n);一个方法:(求1-n的和)。编程:利用该类实现1-50、1-100的和。

package Word;
class ToN{
    int n;
    
    public ToN(int n) {
        this.n = n; //如果n代表1-n的n那么意义可能大一些
    }

    public int sum(){
        int x=0;
        for(int i=1;i<=n;i++){
            x+=i;
        }
        return x;
    }
}
public class Work {
    public static void main(String args[]) {
        ToN obj = new ToN(50);
        System.out.print(obj.sum());
    }
}

11、定义接口Shape,其中包括一个方法size(),设计“矩形”、“圆” 等类实现Shape接口,其size()方法分别表示计算矩形面积、圆面积。分别创建代表“矩形”、“圆” 的两个对象存入一个Shape类型的数组中,通过调用size()方法将数组中各类图形的面积大小输出。
备注:利用该类实现(边长=5,”矩形”,面积)、(半径=2,”圆”,面积)

package Word;
import java.util.Scanner;

interface Shape {
    double size();

}

class Jx implements Shape {
    double w;
    public double size() {
        return w * w;
    }
    public Jx(double w) {
        super();
        this.w = w;
    }
}

class Yuan implements Shape {
    double r;
    public double size() {
        return (Math.PI * r * r);
    }
    public Yuan(double r) {
        super();
        this.r = r;
    }
}

public class Work {
    public static void main(String args[]) {
        Jx jx1 = new Jx(5);
        Yuan y1 = new Yuan(2);
        Shape list [] = {jx1,y1};
        for(int i=0;i<2;i++){
            System.out.println(list[i].size());
        }
    }
}

12、定义一个抽象类-水果,其中包括总价total()抽象方法,编写程序分别创建苹果、橘子两个类,创建两个水果对象存放在一个水果类型的数组中,输出数组中所有水果的类型、重量以及总价。
备注:利用该类实现(”苹果”,2,总价)、(”橘子”,5,总价)

package Word;
import java.util.Scanner;
abstract class Fruit{
    abstract String total();
}
class apple extends Fruit{
    double price=10,number;
    public apple(double number) {
        this.number = number;
    }
    String total() {
        return "苹果" + number + "  个"+"总价:"+price*number;
    }
}
class orange extends Fruit{
    double price=15,number;
    public orange(double number) {
        this.number = number;
    }
    String total() {
        return "橘子" + number + "  个"+"总价:"+price*number;
    }
}
public class Work {
    public static void main(String args[]) {
        apple obj1 = new apple(2);
        orange obj2 = new orange(2);
        fruit list[] = {obj1,obj2};
        for(int i=0;i<2;i++){
            System.out.println(list[i].total());
        }

    }
}

13.编写一个代表三角形的类。其中,三条边为三角型的属性,并封装有求三角形的面积和周长的方法。并设计构造方法和toString() 方法。分别针对三条边为3,4,5和7,8,9的两个三角形进行测试。

package Word;

public class Work {
    public static void main(String[] arg) {
        // Triangle obj = new Triangle(3,4,5);
        Triangle obj = new Triangle(7, 8, 9);
        obj.getArea();
        obj.getPerimeter();
        System.out.print(obj.toString());
    }
}

class Triangle {
    private int a, b, c;

    public Triangle(int d1, int d2, int d3) {
        a = d1;
        b = d2;
        c = d3;
    }

    public void getArea() {
        int p = (a + b + c) / 2;
        double S = Math.sqrt((p * (p - a) * (p - b) * (p - c)));
        System.out.println(S);
    }

    public void getPerimeter() {
        int data;
        data = a + b + c;
        System.out.println(data);
    }

    public String toString() {
        return "a:" + a + "  b:" + b + "  c:" + c;
    }
}

相关文章

网友评论

      本文标题:Java第一学期上机试题

      本文链接:https://www.haomeiwen.com/subject/mpglnctx.html