java第九周上机练习(java第六章上机实践)

发布时间:2022-11-16

本文目录一览:

  1. Java上机作业 声明一个字符串的数组 空间为5个 使用循环接收五个学生的
  2. Java上机作业求帮忙
  3. java上机实验题,要求用java编写,完成其中随便一个就行,急求,能加多少分我就给多少分!!!
  4. java程序设计上机题,求答案
  5. 1、编写一个Application程序【java上机作业,要完整代码,急求!!!!!!!!!!】
  6. 三道JAVA上机编程题,求大神帮忙,做了很久,没做出来

Java上机作业 声明一个字符串的数组 空间为5个 使用循环接收五个学生的

String[] arr = new String[5];
Scanner sca = new Scanner(System.in);
String name = null;
for (int i = 0; i < arr.length; i++) {
    System.out.print("请输入第" + (i + 1) + "个学生姓名:");
    name = sca.next();
    arr[i] = name;
}
System.out.println("现有以下学生");
for (int i = 0; i < arr.length; i++) {
    System.out.println(arr[i] + "\t");
}

Java上机作业求帮忙

作业2:

import java.util.Scanner;
public class Main {
    public static void main(int argc, String[] argv) {
        int num[];
        int n = 0, s = 1;
        Scanner sin = new Scanner(System.in);
        while (sin.hasNextInt()) {
            num[n] = sin.nextInt();
            n++;
        }
        for (int i = 0; i < num.length; i++) {
            s = s * num[i];
        }
        System.out.println(s);
        for (int i = 0; i < num.length; i++) {
            for (int j = 1; j < Math.sqrt(num[i]); j++) {
                if (num[i] % j == 0 && j != 1 && j != num[i]) break;
                System.out.println(num[i] + " ");
            }
        }
    }
}

作业5:

package school;
public class Student {
    private String name;
    private int no;
    public void setName(String name) {
        this.name = name;
    }
    public String getName() {
        return name;
    }
    public void setNo(int no) {
        this.no = no;
    }
    public int getNo() {
        return no;
    }
}
public class ClassStudent extends Student {
    static String className;
    private int grade;
    public void setClassName(String className) {
        this.className = className;
    }
    public String getClassName() {
        return className;
    }
    public void setGrade(int grade) {
        this.grade = grade;
    }
    public int getGrade() {
        return grade;
    }
}
public class Test {
    public static void main(int argc, String[] argv) {
        ClassStudent st1, st2, st3;
        st1.setName("王刚");
        st1.setNo(1009);
        st1.setClassName("Java");
        st1.setGrade(87);
        st2.setName("刘林林");
        st2.setNo(1076);
        st2.setGrade(90);
        st3.setName("石磊");
        st3.setNo(1054);
        st3.setGrade(62);
        int max, min, avg;
        avg = (st1.getGrade() + st2.getGrade() + st3.getGrade()) / 3;
        max = min = st1.getGrade();
        if (st2.getGrade() > max) max = st2.getGrade();
        else min = st2.getGrade();
        if (st3.getGrade() > max) max = st3.getGrade();
        else min = st3.getGrade();
        System.out.println("姓名 学号 课程 成绩");
        System.out.println(st1.getName() + " " + st1.getNo() + " " + st1.getClassName() + " " + st1.getGrade());
        System.out.println(st2.getName() + " " + st2.getNo() + " " + st2.getClassName() + " " + st2.getGrade());
        System.out.println(st3.getName() + " " + st3.getNo() + " " + st3.getClassName() + " " + st3.getGrade());
        System.out.println("最高分:" + max + " " + "最低分:" + min + " " + "平均分:" + avg);
    }
}

作业6:

interface Graphics {
    double area();
    double perimeter();
}
public class Triangle implements Graphics {
    private double a, b, c;
    public Triangle(int a, int b, int c) {
        this.a = a;
        this.b = b;
        this.c = c;
    }
    public double area() {
        double p = perimeter();
        return Math.sqrt(p * (p - a) * (p - b) * (p - c));
    }
    public double perimeter() {
        return a + b + c;
    }
}
public class Square implements Graphics {
    private double a;
    public Square(int a) {
        this.a = a;
    }
    public double area() {
        return a * a;
    }
    public double perimeter() {
        return a * 4;
    }
}
public class Circle implements Graphics {
    private double r;
    public Circle(int r) {
        this.r = r;
    }
    public double area() {
        return Math.PI * r * r;
    }
    public double perimeter() {
        return Math.PI * 2 * r;
    }
}

java上机实验题,要求用java编写,完成其中随便一个就行,急求,能加多少分我就给多少分!!!

package testTime;
import java.util.LinkedList;
public class BinaryTree {
    // 根节点
    private Node<Integer> root;
    // 二叉树中节点数量
    private int size;
    // 无参构造器
    public BinaryTree() {
        root = new Node<>();
    }
    // 数组构造器
    public BinaryTree(int[] values) {
        System.out.print("新建binaryTree:");
        for (int i : values) {
            System.out.print(i);
        }
        System.out.println();
        boolean isLeft = true;
        int len = values.length;
        if (len == 0) return;
        LinkedList<Node<Integer>> queue = new LinkedList<>();
        root = new Node<>(values[0]);
        queue.addLast(root);
        Node parent = null;
        Node current = null;
        for (int i = 1; i < len; i++) {
            current = new Node<>(values[i]);
            queue.addLast(current);
            if (isLeft) parent = queue.getFirst();
            else parent = queue.removeFirst();
            if (isLeft) {
                parent.setLeftChild(current);
                isLeft = false;
            } else {
                parent.setRightChild(current);
                isLeft = true;
            }
        }
    }
    // 递归中序遍历
    public void inorder() {
        System.out.print("binaryTree递归中序遍历:");
        inorderTraverseRecursion(root);
        System.out.println();
    }
    // 层次遍历
    public void layerorder() {
        System.out.print("binaryTree层次遍历:");
        LinkedList<Node<Integer>> queue = new LinkedList<>();
        queue.addLast(root);
        Node<Integer> current = null;
        while (!queue.isEmpty()) {
            current = queue.removeFirst();
            if (current.getLeftChild() != null) queue.addLast(current.getLeftChild());
            if (current.getRightChild() != null) queue.addLast(current.getRightChild());
            System.out.print(current.getValue());
        }
        System.out.println();
    }
    // 获得二叉树深度
    public int getDepth() {
        return getDepthRecursion(root);
    }
    private int getDepthRecursion(Node<Integer> node) {
        if (node == null) return 0;
        int llen = getDepthRecursion(node.getLeftChild());
        int rlen = getDepthRecursion(node.getRightChild());
        int maxlen = Math.max(llen, rlen);
        return maxlen + 1;
    }
    // 递归先序遍历
    public void preorder() {
        System.out.print("binaryTree递归先序遍历:");
        preorderTraverseRecursion(root);
        System.out.println();
    }
    private void inorderTraverseRecursion(Node<Integer> node) {
        if (node.getLeftChild() != null) inorderTraverseRecursion(node.getLeftChild());
        System.out.print(node.getValue());
        if (node.getRightChild() != null) inorderTraverseRecursion(node.getRightChild());
    }
    private void preorderTraverseRecursion(Node<Integer> node) {
        System.out.print(node.getValue());
        if (node.getLeftChild() != null) preorderTraverseRecursion(node.getLeftChild());
        if (node.getRightChild() != null) preorderTraverseRecursion(node.getRightChild());
    }
    // 非递归先序遍历
    public void preorderNoRecursion() {
        System.out.print("binaryTree非递归先序遍历:");
        LinkedList<Node<Integer>> stack = new LinkedList<>();
        stack.push(root);
        Node<Integer> current = null;
        while (!stack.isEmpty()) {
            current = stack.pop();
            System.out.print(current.getValue());
            if (current.getRightChild() != null) stack.push(current.getRightChild());
            if (current.getLeftChild() != null) stack.push(current.getLeftChild());
        }
        System.out.println();
    }
    /**
     * 非递归中序遍历
     * 栈内保存将要访问的元素
     */
    public void inorderNoRecursion() {
        System.out.print("binaryTree非递归中序遍历:");
        LinkedList<Node<Integer>> stack = new LinkedList<>();
        Node<Integer> current = root;
        while (current != null || !stack.isEmpty()) {
            while (current != null) {
                stack.push(current);
                current = current.getLeftChild();
            }
            if (!stack.isEmpty()) {
                current = stack.pop();
                System.out.print(current.getValue());
                current = current.getRightChild();
            }
        }
        System.out.println();
    }
    /**
     * 非递归后序遍历
     * 当上一个访问的结点是右孩子或者当前结点没有右孩子则访问当前结点
     */
    public void postorderNoRecursion() {
        System.out.print("binaryTree非递归后序遍历:");
        Node<Integer> rNode = null;
        Node<Integer> current = root;
        LinkedList<Node<Integer>> stack = new LinkedList<>();
        while (current != null || !stack.isEmpty()) {
            while (current != null) {
                stack.push(current);
                current = current.getLeftChild();
            }
            current = stack.pop();
            while (current != null && (current.getRightChild() == null || current.getRightChild() == rNode)) {
                System.out.print(current.getValue());
                rNode = current;
                if (stack.isEmpty()) {
                    System.out.println();
                    return;
                }
                current = stack.pop();
            }
            stack.push(current);
            current = current.getRightChild();
        }
    }
    public static void main(String[] args) {
        BinaryTree bt = new BinaryTree(new int[]{1, 2, 3, 4, 5, 6, 7, 8});
        bt.inorder();
        bt.preorder();
        bt.layerorder();
        bt.preorderNoRecursion();
        bt.inorderNoRecursion();
        bt.postorderNoRecursion();
        System.out.println("深度为:" + bt.getDepth());
    }
}
class Node<V> {
    private V value;
    private Node<V> leftChild;
    private Node<V> rightChild;
    public Node() {
    }
    public Node(V value) {
        this.value = value;
        leftChild = null;
        rightChild = null;
    }
    public void setLeftChild(Node<V> lNode) {
        this.leftChild = lNode;
    }
    public void setRightChild(Node<V> rNode) {
        this.rightChild = rNode;
    }
    public V getValue() {
        return value;
    }
    public void setValue(V value) {
        this.value = value;
    }
    public Node<V> getLeftChild() {
        return leftChild;
    }
    public Node<V> getRightChild() {
        return rightChild;
    }
}

java程序设计上机题,求答案

public class Point {
    int x;
    int y;
    public Point() {
    }
    public Point(int x, int y) {
        this.x = x;
        this.y = y;
    }
    public Point(int x) {
        this.x = x;
        this.y = x;
    }
    public double distance() { // 求当前点到原点的距离
        return Math.sqrt((x * x + y * y));
    }
    public double distance(int x1, int y1) { // 求当前点到(x1,y1)的距离
        return Math.sqrt((x - x1) * (x - x1) + (y - y1) * (y - y1));
    }
    public double distance(Point other) {
        int x2 = other.x;
        int y2 = other.y;
        return Math.sqrt((x - x2) * (x - x2) + (y - y2) * (y - y2));
    }
}

1、编写一个Application程序【java上机作业,要完整代码,急求!!!!!!!!!!】

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
public class RadioTest extends JFrame {
    private JRadioButton jrb1;
    private JRadioButton jrb2;
    private JLabel jlbl;
    private JPanel jp;
    private JButton jbtn;
    private String jlstr;
    private ButtonGroup bg;
    public RadioTest() {
        jlstr = "你选择的是:";
        this.setTitle("实现单选按钮的效果");
        jrb1 = new JRadioButton("男");
        jrb2 = new JRadioButton("女");
        bg = new ButtonGroup();
        bg.add(jrb1);
        bg.add(jrb2);
        jlbl = new JLabel(jlstr);
        jbtn = new JButton("退出");
        jbtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(1);
            }
        });
        jrb1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (e.getSource() == jrb1) {
                    jlbl.setText(jlstr + jrb1.getText());
                }
            }
        });
        jrb2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (e.getSource() == jrb2) {
                    jlbl.setText(jlstr + jrb2.getText());
                }
            }
        });
        jp = new JPanel();
        jp.add(jrb1);
        jp.add(jrb2);
        jp.add(jlbl);
        jp.add(jbtn);
        this.add(jp);
        this.setBounds(300, 300, 230, 200);
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        RadioTest rt = new RadioTest();
    }
}

三道JAVA上机编程题,求大神帮忙,做了很久,没做出来

第二题

static List<String> allList = new ArrayList<>();
static List<String> txtList = new ArrayList<>();
public static void doDemo() {
    File file = new File("D:" + File.separator);
    if (null == file) return;
    allList.clear();
    txtList.clear();
    listAllFile(file);
    for (String p : allList) {
        System.out.println("file: " + p);
    }
    for (String txt : txtList) {
        System.out.println("txt file: " + txt);
    }
}
public static void listAllFile(File dir) {
    if (null == dir || !dir.exists()) {
        return;
    }
    if (dir.isDirectory()) {
        File[] files = dir.listFiles();
        if (null != files) {
            for (int i = 0; i < files.length; i++) {
                if (files[i].isDirectory()) {
                    listAllFile(files[i]);
                } else {
                    String fileName = files[i].getName();
                    if (fileName.endsWith(".txt")) {
                        txtList.add(files[i].getPath());
                    }
                    allList.add(files[i].getPath());
                }
            }
        }
    } else {
        allList.add(dir.getPath());
    }
}