JAVA上机实验笔记

发布于 / 笔迹 / 9 条评论

JAVA上机实验习题解答,不保证一定是正确的,仅供参考。若文中存在错误,请及时指出。

目录

  1. 实验一:角谷猜想
  2. 实验二:计算e^x
  3. 实验三:复数类
  4. 实验四:圆类
  5. 实验五:整型集合类
  6. 实验六:判断身份证

实验一:角谷猜想

分析

没什么好说的,直接写就行了。

代码

  • JAVA应用程序实现
// 直接证明(3到30000以内的所有数都符合,所以都会输出)

public class Main
{
    public static boolean JiaoGu(int n)
    {
        while (n != 1) // 如果不符合角谷猜想则会陷入死循环
        {
            if (n % 2 == 0)
                n /= 2;
            else
                n = 3 * n + 1;
        }
        return true; // 符合则会返回真
    }

    public static void main(String[] args)
    {
        for (int i = 3; i <= 30000; ++i) // 从3到30000都验证一遍
        {
            if (JiaoGu(i)) // 符合则会输出
                System.out.print(i + " ");
            if (i % 30 == 0) // 换行输出
                System.out.println();
        }
    }
}

// 手动输入(输入一个判断一个)

import java.util.Scanner;

public class Main
{
    public static boolean JiaoGu(int n)
    {
        while (n != 1)
        {
            if (n % 2 == 0)
            {
                System.out.println(n + " / 2 = " + n/2);
                n /= 2;
            }
            else
            {
                System.out.println( "3 * " + n + " + 1 = "  + (3*n+1));
                n = 3 * n + 1;
            }
        }
//        System.out.println(n);
        return true;
    }

    public static void main(String[] args)
    {
        Scanner cin = new Scanner(System.in);
        System.out.print("请输入3~30000以内的数:");
        int x = cin.nextInt();
        if (JiaoGu(x))
            System.out.println(x + " 符合角谷猜想。");
        else
            System.out.println(x + " 不符合角谷猜想。");
    }
}

/*
请输入3~30000以内的数:11
3 * 11 + 1 = 34
34 / 2 = 17
3 * 17 + 1 = 52
52 / 2 = 26
26 / 2 = 13
3 * 13 + 1 = 40
40 / 2 = 20
20 / 2 = 10
10 / 2 = 5
3 * 5 + 1 = 16
16 / 2 = 8
8 / 2 = 4
4 / 2 = 2
2 / 2 = 1
11符合角谷猜想
*/

  • JAVA小应用程序实现
// 输入后判断,控制台输出数的变化过程

import java.awt.*;
import java.applet.*;

public class Main extends Applet
{
    Label info;
    TextField input, output;
    int num, nums;
    Graphics g;

    public void init( )
    {
        info = new Label("请输入3~30000以内的数");
        input = new TextField(10);
//      output = new TextField(10);
        add(info);
        add(input);
//      add(output);
    }

    public boolean action(Event e , Object o)
    {
        if (e.target==input)
        {
            num=Integer.parseInt(input.getText( ));
            nums = num;
            int xpos = 20, ypos = 20;
            System.out.println("控制台" + num + "的变化过程如下:");
            while (num != 1)
            {
                if (num % 2 == 0)
                {
                    showStatus(num + " / 2 = " + num/2);
                    System.out.println(num + " / 2 = " + num/2);
//                  g.drawString(num + " / 2 = " + num/2, xpos, ypos); // error
//                  ypos += 20;
                    num /= 2;
                }
                else
                {
                    showStatus("3 * " + num + " + 1 = "  + (3*num+1));
                    System.out.println( "3 * " + num + " + 1 = "  + (3*num+1));
//                  g.drawString("3 * " + num + " + 1 = "  + (3*num+1), xpos, ypos); // error
//                  ypos += 20;
                    num = 3*num + 1;
                }
            }
            showStatus(nums + " 符合角谷猜想, 最后的值为:" + num);
        }
        return true;
    }
} 


实验二:计算e^x

分析

没什么好讲的,计算一下阶乘就行(注意参数问题)。

代码

  • 应用程序版
import java.util.Scanner;
import java.math.*;

public class EandX
{
    public static int fac(int n)
    {
        int mul = 1;
        for (int i = 2; i <= n; ++i)
            mul *= i;
        System.out.println(mul);
        return mul;
    }

    public static void main(String[] args)
    {
        Scanner cin = new Scanner(System.in);
        int x, n;
        x = cin.nextInt();
        n = cin.nextInt();
        double sum = 1.0;
        for (int i = 1; i <= n; ++i)
            sum += Math.pow(x*1.0, i*1.0) / fac(i)*1.0;
//        System.out.println(sum);
        System.out.println(String.format("%.10f", sum)); // 保留10位小数
    }
}
  • 小程序版本
import java.awt.*;
import java.applet.*;

public class EandX extends Applet
{
    Label info1, info2;
    TextField input1, input2;
    int n, x;
    double sum;

    public void init( )
    {
        info1 = new Label("请输入n:");
        input1 = new TextField(20);
        info2 = new Label("请输入x:");
        input2 = new TextField(20);

        add(info1);
        add(input1);
        add(info2);
        add(input2);
    }

    public int fac(int n)
    {
        int mul = 1;
        for (int i = 2; i <= n; ++i)
            mul *= i;
        System.out.println(mul);
        return mul;
    }

    public boolean action(Event e , Object o)
    {
        sum = 1.0;
        if (e.target == input1 || e.target == input2)
        {
            n = Integer.parseInt(input1.getText( ));
            x = Integer.parseInt(input2.getText( ));
            for (int i = 1; i <= n; ++i)
                sum += Math.pow(x*1.0, i*1.0) / fac(i)*1.0;
            showStatus("e的" + x + "次方的值为:" + sum);
        }
        return true;
    }
}


实验三:复数类

分析

没什么好讲的,类的简单实现。

代码

import java.util.*;

class complex
{
    int realPart, imagPart;
    complex(int _real, int _imag)
    {
        realPart = _real;
        imagPart = _imag;
    }
    void add(complex other)
    {
        this.realPart += other.realPart;
        this.imagPart += other.imagPart;
    }
    void minus(complex other)
    {
        this.realPart -= other.realPart;
        this.imagPart -= other.imagPart;
    }
}

public class Main
{
    public static void main(String[] args)
    {
        Scanner cin = new Scanner(System.in);
        int a1, b1, a2, b2;
        System.out.print("Please enter the realPart and imagPart of the first complex: ");
        a1 = cin.nextInt();
        b1 = cin.nextInt();
        System.out.print("Please enter the realPart and imagPart of the second complex: ");
        a2 = cin.nextInt();
        b2 = cin.nextInt();
        complex a = new complex(a1, b1);
        complex b = new complex(a2, b2);
        a.add(b);
        if (a.realPart == a1 + a2 && a.imagPart == b1 + b2)
            System.out.println("The result of add is right, they are: " + a.realPart + " " + a.imagPart);
        else
            System.out.println("Error, please check your program.");
        a1 = a1 + a2;
        b1 = b1 + b2;
        a.minus(b);
        if (a.realPart == a1 - a2 && a.imagPart == b1 - b2)
            System.out.println("The result of add is right, they are: " + a.realPart + " " + a.imagPart);
        else
            System.out.println("Error, please check your program.");
    }
}
/*
Please enter the realPart and imagPart of the first complex: 2 3
Please enter the realPart and imagPart of the second complex: 4 5
The result of add is right, they are: 6 8
The result of add is right, they are: 2 3
*/


实验四:圆类

分析

依然没什么好讲的,类的简单实现。

代码

import java.util.*;

class circle
{
    int radius;
    circle (int r)
    {
        radius = r;
    }
    double circum()
    {
        return 2 * Math.PI * radius;
    }
    double area()
    {
        return Math.PI * radius * radius;
    }
}

public class Main
{
    public static void main(String[] args)
    {
        Scanner cin = new Scanner(System.in);
        int r;
        System.out.print("PLease enter the radius of the circle: ");
        r = cin.nextInt();
        if (r < 0)
            System.out.println("Error, the radius of the circle can't be a negative number.");
        else
        {
            circle c = new circle(r);
            double circum = c.circum();
            double area = c.area();
            System.out.println("The circumference of the circle is : " + String.format("%.4f", circum));
            System.out.println("THe area of the circle is : " + String.format("%.4f", area));
        }
    }
}
/*
PLease enter the radius of the circle: 2
The circumference of the circle is : 12.5664
THe area of the circle is : 12.5664
*/


实验五:整型集合类

分析

① 使用boolen数组来对集合中的元素进行标记判断集合是否相等
② 交集判断可以使用boolen数组或者双重循环遍历
③ 并集使用boolen数组
④ 插入只需后移
⑤ 删除只需前移

代码

import java.util.*;
import java.math.*;
import java.io.*;

class integerSet
{
    Scanner cin = new Scanner(System.in);

    int n; // 长度
    int array[] = new int[20]; // 基本元素
    int Sarray[] = new int[20]; // 交集
    int Uarray[] = new int[20]; // 并集

    void init() // 初始化
    {
        System.out.print("Please enter the length of the sets: ");
        n = cin.nextInt();
        System.out.print("Now you can enter their values individually: ");
        for (int i = 0; i < n; ++i)
            array[i] = cin.nextInt();
    }

    void print() // 输出
    {
        System.out.print("The elements in the sets are: ");
        for (int i = 0; i < n; ++i)
            System.out.print(array[i] + " ");
        System.out.println();
    }

    boolean isEqual(integerSet other) // 是否相等
    {
        if (this.n != other.n)
            return false;
        boolean tmp1[] = new boolean[100];
        boolean tmp2[] = new boolean[100];
        for (int i = 0; i < this.n; ++i)
        {
            tmp1[this.array[i]] = true;
            tmp2[other.array[i]] = true;
        }
        for (int i = 0; i < 100; ++i)
        {
            if (tmp1[i] != tmp2[i])
                return false;
        }
        return true;
    }

    void Union(integerSet other) // 并集
    {
        boolean tmp[] = new boolean[100];
        for (int i = 0; i < this.n; ++i)
            tmp[this.array[i]] = true;
        for (int j = 0; j < other.n; ++j)
            tmp[other.array[j]] = true;
        int k = 0;
        for (int i = 0; i < 100; ++i)
        {
            if (tmp[i])
                Uarray[k++] = i;
        }
        System.out.print("The elements in the union of these two sets are: ");
        for (int i = 0; i < k; ++i)
            System.out.print(Uarray[i] + " ");
        System.out.println();
    }

    void Inter(integerSet other) // 交集
    {
        boolean tmp[] = new boolean[100];
        for (int i = 0; i < this.n; ++i)
        {
            for (int j = 0; j < other.n; ++j)
            {
                if (this.array[i] == other.array[j])
                    tmp[this.array[i]] = true;
            }
        }
        int k = 0;
        for (int i = 0; i < 100; ++i)
        {
            if (tmp[i])
                Sarray[k++] = i;
        }
        System.out.print("The elements in the intersection of these two sets are: ");
        for (int i = 0; i < k; ++i)
            System.out.print(Sarray[i] + " ");
        System.out.println();
    }

    void insert() // 插入
    {
        int pos, value;
        System.out.print("Please enter the position of the inserted element: ");
        pos = cin.nextInt();
        System.out.print("Please enter the vlaue of the inserted element: ");
        value = cin.nextInt();
        for (int i = n; i > pos; --i)
            array[i] = array[i-1];
        array[pos] = value;
        ++n;
        this.print();
    }

    void remove() // 删除
    {
        int pos;
        System.out.print("Please enter the position of the deleted element: ");
        pos = cin.nextInt();
        for (int i = pos; i < n - 1; ++i)
            array[i] = array[i+1];
        --n;
        this.print();
    }
}

public class Main
{
    public static void main(String []args)
    {
        integerSet a = new integerSet();
        integerSet b = new integerSet();
        a.init();
        b.init();
        if (a.isEqual(b))
            System.out.println("The two sets are equal.");
        else
            System.out.println("The two sets are not equal.");
        a.Union(b); // 操作
        a.Inter(b);
        a.insert();
        a.remove();
    }
}

/*
Please enter the length of the sets: 7
Now you can enter their values individually: 1 2 3 4 5 6 7
Please enter the length of the sets: 8
Now you can enter their values individually: 2 3 5 7 9 11 13 15
The two sets are not equal.
The elements in the union of these two sets are: 1 2 3 4 5 6 7 9 11 13 15 
The elements in the intersection of these two sets are: 2 3 5 7 
Please enter the position of the inserted element: 1
Please enter the vlaue of the inserted element: 2018
The elements in the sets are: 1 2018 2 3 4 5 6 7 
Please enter the position of the deleted element: 1
The elements in the sets are: 1 2 3 4 5 6 7 
*/

说明

我的插入和删除都是从0位置开始的,要从1位置开始的话,只需将输入的位置pos--就行,然后我把插入都删除方法都在集合类中完成,主类中直接调用,判断是否相等的话我没有完全在集合类中进行,输出在主类中。本题数据量小可以使用标记数组和双重循环,不用考虑别的问题。


实验六:判断身份证

一个合法的身份证号码由17位地区、日期编号和顺序编号加1位校验码组成。校验码的计算规则如下:
首先对前17位数字加权求和,权重分配为:{7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};然后将计算的和对11取模得到值Z;最后按照以下关系对应Z值与校验码M的值:
Z:0 1 2 3 4 5 6 7 8 9 10
M:1 0 X 9 8 7 6 5 4 3 2
现在给定一些身份证号码,请你验证校验码的有效性。

分析

没什么好讲的,将计算得到的结果取模后比对一下就行。

代码

import java.util.*;
import java.math.*;
import java.io.*;

public class Main
{

    static int a[] = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
    static String M = "10X98765432";

    static boolean IsValid(String s) // 判断是否符合校验
    {
        int sum = 0;
        int length = s.length();
        for (int i = 0; i < length - 1; ++i)
            sum += (s.charAt(i) - '0') * a[i];
        if (M.charAt(sum % 11) == s.charAt(length - 1))
            return true;
        return false;
    }

    static boolean Isnum(String s) // 判断前十七位是否为数字
    {
        int length = s.length();
        for (int i = 0; i < length - 1; ++i)
            if (!Character.isDigit(s.charAt(i)))
                return false;
        return true;
    }

    public static void main(String []args)
    {
        Scanner cin = new Scanner(System.in);
        System.out.print("请输入查询的身份证号:");
        String s = cin.next();
        if (IsValid(s) && Isnum(s))
            System.out.println("该身份证号合法");
        else
            System.out.println("该身份证号非法");
    }
}

说明

可以直接分别输入17位数字,用数组存储,比较简单,我用的是字符串,判断的时候稍微处理一下就行。


To be continued.
2018-04-12 星期四

转载原创文章请注明,转载自: 太傅 » JAVA上机实验笔记
  1. TaiFu_S

    保持“增量”更新 doge1

  2. CongTsang

    啊,你怎么不等我!尴尬中刀

    1. TaiFu_S
      @CongTsang

      喷 你太慢了!

  3. Shawn

    越来越棒了|´・ω・)ノ

    1. TaiFu_S
      @Shawn

      嘿嘿,蟹蟹 太开心

      1. Shawn
        @TaiFu_S

        你关了pjax?

        1. TaiFu_S
          @Shawn

          对,因为那个表情的原因...没修bug

  4. Zombie.

    可以,很不错!doge

    1. TaiFu_S
      @Zombie.

      嘿嘿吐舌