首页 > 编程知识 正文

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

时间:2023-12-24 12:05:48 阅读:320996 作者:EEFR

本文目录一览:

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

String[] arr=new String[5];

Scanner sca=new Scanner(System.in);

String name=null;

for(int i=0;iarr.lenth();i++){

System.out.print("请输入第"+(i+1)+"个学生姓名:");

name=sca.next();

arr[i]=name;

}

System.out.println("现有以下学生");

for(int i=0;iarr.lenth();i++){

System.out.println(arr[i]+"t");

}

Java上机作业求帮忙

作业2:

import java.util.Scanner;

public class Main

{

public static void main(int argc,Sring[] argv)

{

int num[];

int n=0,s=1;

Scanner sin=new Scanner(System.in);

while(sin.haveInt())

{

num[n]=sin.nextInt();

n++;

}

for(int i=0;inum.length();i++)

s=s*num[i];

System.out.println(s);

for(int i=0;inum.length();i++)

for(int j=1;jMath.sqrt(num[i]);j++)

{

if(num[i]%j==0j!=1j!=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.get

className()+" "+st1.getGrade());

System.out.println(st2.getName()+" "+st2.getNo()+" "+st2.get

className()+" "+st2.getGrade());

System.out.println(st3.getName()+" "+st3.getNo()+" "+st3.get

className()+" "+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 doubler;

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 NodeInteger root;

//二叉树中节点数量

private int size;

//无参构造器

public BinaryTree() {

root = new NodeInteger();

}

//数组构造器

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 ;

LinkedListNodeInteger queue = new LinkedListNodeInteger();

root = new NodeInteger(values[0]);

queue.addLast(root);

Node parent = null;

Node current = null;

for (int i=1; ilen; i++) {

current = new NodeInteger(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层次遍历:");

LinkedListNodeInteger queue = new LinkedListNodeInteger();

queue.addLast(root);

NodeInteger 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(NodeInteger 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(NodeInteger node) {

// TODO Auto-generated method stub

if (node.getLeftChild() != null)

inorderTraverseRecursion(node.getLeftChild());

System.out.print(node.getValue());

if (node.getRightChild() != null)

inorderTraverseRecursion(node.getRightChild());

}

private void preorderTraverseRecursion(NodeInteger 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非递归先序遍历:");

LinkedListNodeInteger stack = new LinkedListNodeInteger();

stack.push(root);

NodeInteger 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非递归中序遍历:");

LinkedListNodeInteger stack = new LinkedListNodeInteger();

NodeInteger 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非递归后序遍历:");

NodeInteger rNode = null;

NodeInteger current = root;

LinkedListNodeInteger stack = new LinkedListNodeInteger();

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 NodeV{

private V value;

private NodeV leftChild;

private NodeV rightChild;

public Node(){

};

public Node(V value) {

this.value = value;

leftChild = null;

rightChild = null;

}

public void setLeftChild(NodeV lNode) {

this.leftChild = lNode;

}

public void setRightChild(NodeV rNode) {

this.rightChild = rNode;

}

public V getValue() {

return value;

}

public void setValue(V value) {

this.value = value;

}

public NodeV getLeftChild() {

return leftChild;

}

public NodeV 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) {

// TODO Auto-generated method stub

System.exit(1);

}

});

jrb1.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

// TODO Auto-generated method stub

if(e.getSource()==jrb1){

jlbl.setText(jlstr+jrb1.getText());

}

}

});

jrb2.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

// TODO Auto-generated method stub

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) {

// TODO Auto-generated method stub

RadioTest rt = new RadioTest();

}

}

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

第二题

一个doDemo方法搞定

这个是列出D盘下所有文件及文件目录,然后再列出所有的.txt后缀的文件。

static ListString allList = new ArrayListString();

static ListString txtList = new ArrayListString();

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());

}

}

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。