java实验题目汇总下载(java实验内容)

发布时间:2022-11-09

本文目录一览:

  1. 一个JAVA课程实验的题目,急要,要有注释
  2. 求代码,java实验,题目如图
  3. JAVA 实验题
  4. 求解一道Java实验题,给出一段代码,要求把该代码补充完整使其可以运行,具体要求如下

一个JAVA课程实验的题目,急要,要有注释

//第二题
import java.util.Scanner;
public class Test {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入数据以空格隔开:");
        String[] str = (sc.nextLine()).split("\\s+");
        double[] ds = new double[2];
        for(int i = 0; i < str.length; i++){
            double d = Double.parseDouble(str[i]);
            try{
                if(d > 100 || d < 0) throw new MyException();
            }catch(MyException e){
                e.printStackTrace();
            }
            ds[i] = d;
        }
        for(double d : ds)
            System.out.println(d);
    }
}
class MyException extends Exception{
    public MyException(){
        System.out.println("数据大于100或小于0");
    }
}

求代码,java实验,题目如图

import java.util.Scanner;
import java.util.Stack;
public class DFS {
    // 存储节点信息
    private char[] vertices;
    // 存储边信息(邻接矩阵)
    private int[][] arcs;
    // 图的节点数
    private int vexnum;
    // 记录节点是否已被遍历
    private boolean[] visited;
    // 初始化
    public DFS(int n) {
        vexnum = n;
        vertices = new char[n];
        arcs = new int[n][n];
        visited = new boolean[n];
        for(int i = 0; i < vexnum; i++) {
            for(int j = 0; j < vexnum; j++) {
                arcs[i][j] = 0;
            }
        }
    }
    // 添加边(无向图)
    public void addEdge(int i, int j) {
        // 边的头尾不能为同一节点
        if(i == j)
            return;
        arcs[i - 1][j - 1] = 1;
        arcs[j - 1][i - 1] = 1;
    }
    // 设置节点集
    public void setVertices(char[] vertices) {
        this.vertices = vertices;
    }
    // 设置节点访问标记
    public void setVisited(boolean[] visited) {
        this.visited = visited;
    }
    // 打印遍历节点
    public void visit(int i) {
        System.out.print(vertices[i] + " ");
    }
    // 从第i个节点开始深度优先遍历
    private void traverse(int i) {
        // 标记第i个节点已遍历
        visited[i] = true;
        // 打印当前遍历的节点
        visit(i);
        // 遍历邻接矩阵中第i个节点的直接联通关系
        for(int j = 0; j < vexnum; j++) {
            // 目标节点与当前节点直接联通,并且该节点还没有被访问,递归
            if(arcs[i][j] == 1 && visited[j] == false) {
                traverse(j);
            }
        }
    }
    // 图的深度优先遍历(递归)
    public void DFSTraverse(int start) {
        // 初始化节点遍历标记
        for(int i = 0; i < vexnum; i++) {
            visited[i] = false;
        }
        // 从没有被遍历的节点开始深度遍历
        for(int i = start - 1; i < vexnum; i++) {
            if(visited[i] == false) {
                // 若是连通图,只会执行一次
                traverse(i);
            }
        }
    }
    // 图的深度优先遍历(非递归)
    public void DFSTraverse2(int start) {
        // 初始化节点遍历标记
        for(int i = 0; i < vexnum; i++) {
            visited[i] = false;
        }
        Stack<Integer> s = new Stack<Integer>();
        for(int i = start - 1; i < vexnum; i++) {
            if(!visited[i]) {
                // 连通子图起始节点
                s.add(i);
                do {
                    // 出栈
                    int curr = s.pop();
                    // 如果该节点还没有被遍历,则遍历该节点并将子节点入栈
                    if(visited[curr] == false) {
                        // 遍历并打印
                        visit(curr);
                        visited[curr] = true;
                        // 没遍历的子节点入栈
                        for(int j = vexnum - 1; j >= 0; j--) {
                            if(arcs[curr][j] == 1 && visited[j] == false) {
                                s.add(j);
                            }
                        }
                    }
                } while(!s.isEmpty());
            }
        }
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N, M, S;
        while(true) {
            System.out.println("输入N M S,分别表示图G的结点数,边数,搜索的起点:");
            String line = sc.nextLine();
            if(!line.matches("^\\s*([1-9]\\d?|100)(\\s+([1-9]\\d?|100)){2}\\s*$")) {
                System.out.print("输入错误,");
                continue;
            }
            String[] arr = line.trim().split("\\s+");
            N = Integer.parseInt(arr[0]);
            M = Integer.parseInt(arr[1]);
            S = Integer.parseInt(arr[2]);
            break;
        }
        DFS g = new DFS(N);
        char[] vertices = new char[N];
        for(int i = 0; i < N; i++) {
            vertices[i] = (i + 1 + "").charAt(0);
        }
        g.setVertices(vertices);
        for(int m = 0; m < M; m++) {
            System.out.println("输入图G的第" + (m + 1) + "条边,格式为“i j”,其中i,j为结点编号(范围是1~N)");
            String line = sc.nextLine();
            if(!line.matches("^\\s*([1-9]\\d?|100)\\s+([1-9]\\d?|100)\\s*$")) {
                System.out.print("输入错误,");
                m--;
                continue;
            }
            String[] arr = line.trim().split("\\s+");
            int i = Integer.parseInt(arr[0]);
            int j = Integer.parseInt(arr[1]);
            g.addEdge(i, j);
        }
        sc.close();
        System.out.print("深度优先遍历(递归):");
        g.DFSTraverse(S);
        System.out.println();
        System.out.print("深度优先遍历(非递归):");
        g.DFSTraverse2(S);
    }
}

JAVA 实验题

// LeadMain.java
import java.util.*;
public class LeadMain {
    public static void main(String args[]){
        MyDate mydate = new MyDate();
        int y;
        Scanner yreader = new Scanner(System.in);
        System.out.println("从键盘输入年份:");
        y = yreader.nextInt();
        mydate.setYear(y);
        int m;
        Scanner mreader = new Scanner(System.in);
        System.out.println("从键盘输入月份:");
        m = mreader.nextInt();
        mydate.setMonte(m);
        mydate.LeapToJudge();
        mydate.DaysOfTheMonth();
        mydate.Tomorrow();
    }
}
// MyDate.java
import java.util.*;
public class MyDate {
    private int year;
    private int monte;
    private int day;
    public boolean LeapToJudge(){
        if((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)){
            return true;
        } else {
            return false;
        }
    }
    public void DaysOfTheMonth(){
        if(LeapToJudge()){
            System.out.println(year + "年是闰年!");
            if(monte == 2){
                System.out.println(year + "年" + monte + "月的天数为:29天");
            } else {
                if(monte == 1 || monte == 3 || monte == 5 || monte == 7 || monte == 8 || monte == 10 || monte == 12){
                    System.out.println(year + "年" + monte + "月的天数为:31天");
                } else {
                    System.out.println(year + "年" + monte + "月的天数为:30天");
                }
            }
        } else {
            System.out.println(year + "年不是闰年!");
            if(monte == 2){
                System.out.println(year + "年" + monte + "月的天数为:28天");
            } else {
                if(monte == 1 || monte == 3 || monte == 5 || monte == 7 || monte == 8 || monte == 10 || monte == 12){
                    System.out.println(year + "年" + monte + "月的天数为:31天");
                } else {
                    System.out.println(year + "年" + monte + "月的天数为:30天");
                }
            }
        }
    }
    public void Tomorrow(){
        Date today = new Date();
        Date tomorrow = new Date(today.getTime() + 24 * 3600 * 1000);
        System.out.println("明天的此刻时间是:" + tomorrow);
    }
    public void setYear(int years){
        year = years;
    }
    public void setMonte(int montes){
        monte = montes;
    }
}

求解一道Java实验题,给出一段代码,要求把该代码补充完整使其可以运行,具体要求如下

package xinguan;
abstract class Operation { //抽象类
    public static double numberA = 0;
    public static double numberB = 0;
    abstract double getResult(); //抽象方法
}
class OperationADD extends Operation {
    @Override
    double getResult() {
        return numberA + numberB;
    }
}
class OperationSUB extends Operation {
    @Override
    double getResult() {
        return numberA - numberB;
    }
}
class OperationMUL extends Operation {
    @Override
    double getResult() {
        return numberA * numberB;
    }
}
class OperationDIV extends Operation {
    @Override
    double getResult() {
        return numberA / numberB;
    }
}
class OperationFactory {
    public static Operation createOperate(char operate) {
        Operation oper = null;
        switch (operate) {
            case '+':
                oper = new OperationADD();
                break;
            case '-':
                oper = new OperationSUB();
                break;
            case '*':
                oper = new OperationMUL();
                break;
            case '/':
                oper = new OperationDIV();
                break;
        }
        return oper;
    }
}
public class CalculateDemo {
    /**
     * @param args
     */
    public static void main(String[] args) {
        Operation operADD = OperationFactory.createOperate('+');
        Operation operSUB = OperationFactory.createOperate('-');
        Operation operMUL = OperationFactory.createOperate('*');
        Operation operDIV = OperationFactory.createOperate('/');
        operADD.numberA = 15.0;
        operADD.numberB = 3;
        System.out.println(operADD.getResult());
        System.out.println(operSUB.getResult());
        System.out.println(operMUL.getResult());
        System.out.println(operDIV.getResult());
    }
}

因为抽象类是静态方法,所以给operADD那个对象赋值一次就能获得所有结果。要是去掉static,那么就需要每个对象赋值。现在基本满足你的要求了。