java上级实验及源代码 java上机实验报告
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 上机实验,设计一个类,具体要求:
package com.tx.DATI;
import java.util.Scanner;
public class PaiXu {
public static void main(String[] args) {
System.out.println("数组一组数字中间用空格隔开");
Scanner sc = new Scanner(System.in);
String s=sc.nextLine();
String[] ss= s.split(" ");
for(String sss:ss) {
System.out.print(sss+"\t");
}
System.out.println();
String[] c=ss;
int sum=0;
for(int i=0; ic.length; i++) {
sum+=Integer.valueOf(c[i]);
}
System.out.println("和:"+sum);
int max=Integer.valueOf(c[0]);
for(int i=0; ic.length; i++) {
if(Integer.valueOf(c[i])max) {
max=Integer.valueOf(c[i]);
}
}
System.out.println("最大:"+max);
int min=Integer.valueOf(c[0]);
for(int i=0; ic.length; i++) {
if(Integer.valueOf(c[i])min) {
min=Integer.valueOf(c[i]);
}
}
System.out.println("最xiao:"+min);
System.out.println("从小到大:");
sort(c);
for(String d:c) {
System.out.println(d);
}
}
private static void sort(String[] c) {
for(int i=0; ic.length; i++) {
for(int j=0; jc.length-i-1; j++) {
if(Integer.valueOf(c[j])Integer.valueOf(c[j+1])) {
String temp=c[j];
c[j]=c[j+1];
c[j+1]=temp;
}
}
}
}
}
实验一:编写类文件Test1.java,程序源代码如下,请对其编译,并排错运行。
public class test1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
StaticDemo st=new StaticDemo();
System.out.println("静态变量x="+st.getX());
System.out.println("实例变量y="+st.getY());
StaticDemo a= new StaticDemo();
StaticDemo b= new StaticDemo();
a.setX(1); a.setY(2);
b.setX(3); b.setY(4);
System.out.println("静态变量a.x="+a.getX());
System.out.println("实例变量a.y="+a.getY());
System.out.println("静态变量b.x="+b.getX());
System.out.println("实例变量b.y="+b.getY());
}
}
class StaticDemo {
static int x;
int y;
public static int getX() { return x; }
public static void setX(int x) { StaticDemo.x = x; }
public int getY() { return y; }
public void setY(int y) { this.y = y; }
}
这才是正确的,需要注意的是:静态变量可以用类名.方法名 而实例变量时通过对象.方法名
网站名称:java上级实验及源代码 java上机实验报告
当前网址:http://cdiso.cn/article/ddgihjs.html