「java上机试题」java机试题一般考哪些知识

博主:adminadmin 2022-11-22 08:18:08 51

本篇文章给大家谈谈java上机试题,以及java机试题一般考哪些知识对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

java上机面试题,求帮助

public class EncodeAndDecode {

/**

* 译码

*

* @param str

*            要译码的字符串

* @return 译码后的字符串

*/

public String encode(String str) throws StringLenException{

StringBuilder sb = new StringBuilder();

if(null == str) {

throw new StringLenException("字符串末初始化!");

}

int n = str.length();

if(n =0) {

throw new StringLenException("字符串不能为空!");

}

char c = 0;

int k = 0;

for (int i = 0; i  n; i++) {

// 获得当前字符

c = str.charAt(i);

if (c = '1'  c = '9'  i != n-1) {    // c是1-9的数字, 且in-1   复制(k+1)次后面的一个字符

k = c -'0' + 1;

for(int j=0; jk; j++) {

sb.append(str.charAt(i+1));

}

} else if (c == '_') {                   // c除1-9,且为”_”         转换为”\Ul”

sb.append("\\UL");

}else{                                   //其余                                       复制该字符

sb.append(c);

}

sb.append("_");

}

return sb.deleteCharAt(sb.length()-1).toString();

}

/**

* 解码

*

* @param str

*            要解码的字符串

* @return 解码后的字符串

*/

public String decode(String str) {

StringBuilder sb = new StringBuilder();

if(null == str) {

throw new StringLenException("字符串末初始化!");

}

if(sb.length() =0) {

throw new StringLenException("字符串不能为空!");

}

String strs[] = str.split("_");

char c = 0;

int k = 0;

int n = strs.length;

if(n == 1) {

sb.append(str);

} else {

for(int i=0; in; i++) {

k = strs[i].length();

c = strs[i].charAt(0);

if(1 ==k) {             //k==1,  将该字符原样复原

sb.append(c);

} else {                //k1,

if(strs[i].equals("\\UL")) {//strs[i] == ”\Ul”,  转换为”_”

sb.append("_");

} else {                    // strs[i] != ”\Ul”,  转换为k(k = strs[i].length()-1)

sb.append(k-1);

}

}

}

}

return sb.toString();

}

/**

* @param args

*/

public static void main(String[] args) {

String s = "24ab_2t2";

//      String s = "04ab_2t2";

//      String s = "1a0b_2t2";

//      String s = "aaab_2t2";

//      String s = "24ab_2335t2";

//      String s = "240ab_";

/*EncodeAndDecode encode = new EncodeAndDecode();

String s1 = encode.encode(s);

System.out.println("encode:" + s1);

String s2 = encode.decode(s1);

System.out.println("decode:" + s2);

*/

}

}

public class StringLenException extends RuntimeException {

public StringLenException() {

super();

}

public StringLenException(String message) {

super(message);

}

}

「java上机试题」java机试题一般考哪些知识

JAVA上机题目5

package com.sql.animal;

public class Pet {

String name="Pet";

void cry()

{

System.out.println(name+"are crying now.");

}

void eat()

{

System.out.println(name+"are eating now.");

}

}

楼主这是第一个类

我一个一个类提交给你~~~ok?

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

}

}

java面试的上机试题一般都是哪些?

1、单表增删改查

2、几种排序

3、金字塔打印

反正都是简单的一些东西,很少公司面试要求上级的

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

}

}

java上机试题的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java机试题一般考哪些知识、java上机试题的信息别忘了在本站进行查找喔。

The End

发布于:2022-11-22,除非注明,否则均为首码项目网原创文章,转载请注明出处。