java经典上机编程题(java上机题 请问这道题应该怎么写)

本文目录
- java上机题 请问这道题应该怎么写
- java上机实验题,要求用java编写,完成其中随便一个就行,急求,能加多少分我就给多少分!!!
- 简单的Java编程题
- 求助大神!!JAVA上机题!!!
- 简单java编程题,运行成功追加20
java上机题 请问这道题应该怎么写
这道JAVA编程题代码要怎么写求代码
编写一个飞机(Plane)类,包含以下属性:
域:初始位置,初始速度,加速度
方法:到达某个位置需要的时间public double arrive(double 目标位置){return 时间}
两个飞机追及时间public double meet(Plane 另一个飞机){return 追及时间}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package test;
public class Plane {
double startPos;
double startSpeed;
double advance;
public Plane(double startPos,double startSpeed,double advance)
{
this.startPos=startPos;
this.startSpeed=startSpeed;
this.advance=advance;
}
public double arrive(double destPos)
{
double time;
time = (-startSpeed+(Math.sqrt(startSpeed*startSpeed+2*advance*(destPos-startPos))))/(advance);
return time;
}
public double chase(Plane plane)
{
double distance = plane.startPos-this.startPos;
double dspeed=-(plane.startSpeed-this.startSpeed);
double dadv = -(plane.advance-this.advance);
double time=0.0;
time=(-dspeed+(Math.sqrt(dspeed*dspeed+2*dadv*(distance))))/dadv;
return time;
}
public static void main(String args) {
Plane p1 = new Plane(1.0,2.0,0.5);
System.out.println(p1.arrive(3.0));
System.out.println(p1.chase(new Plane(5.0,1.5,0.3)));
}
}
以上是代码测试
java上机实验题,要求用java编写,完成其中随便一个就行,急求,能加多少分我就给多少分!!!
package testTime;
import java.util.LinkedList;
public class BinaryTree {
//根节点
private Node《Integer》 root;
//二叉树中节点数量
private int size;
//无参构造器
public BinaryTree() {
root = new Node《Integer》();
}
//数组构造器
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《Node《Integer》》();
root = new Node《Integer》(values);
queue.addLast(root);
Node parent = null;
Node current = null;
for (int i=1; i《len; i++) {
current = new Node《Integer》(values);
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《Node《Integer》》();
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) {
// 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(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《Node《Integer》》();
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》》();
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《Node《Integer》》();
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编程题
import javax.swing.*;
import java.awt.*;
public class TableDemo
{
private JFrame f;
private JTable table;
private String str;
private String column={"姓名","性别","籍贯","年龄"};
public TableDemo()
{
f=new JFrame();
Container con=f.getContentPane();
con.setLayout(new FlowLayout());
str=new String;
str="张先生";
str="男";
str="河北省邯郸市";
str="23";
table=new JTable(str,column);
con.add(new JScrollPane(table));
f.setSize(500,500);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String args)
{
new TableDemo();
}
}
希望这个对你有所帮助。
good luck to you!
求助大神!!JAVA上机题!!!
import java.util.Scanner;
public class Noname2
{
public static void main(String args) {
Scanner br = new Scanner(System.in);
String s = br.next();
char temp = s.toCharArray();
for(int i = temp.length - 1; i 》= 0; i--)
{
if(temp 《= 122)
{
temp - 32);
System.out.print(temp);
}
else
{
if(temp 《= 90)
{
temp + 32);
System.out.print(temp);
}
else
System.out.print(temp);
}
}
System.out.println();
}
}
简单java编程题,运行成功追加20
1)用equals()方法判断,Test t=new Test(2,4); Test t1=new Test(2,4);是否相等。不相等
public class Test {
private int a;
private int b;
public Test(int a, int b){
this.a = a;
this.b = b;
}
public static void main(String args) {
Test t=new Test(2,4);
Test t1=new Test(2,4);
boolean isEqual = t.equals(t1);
System.out.println(isEqual);
}
}
2)设计一种手段,让t.equals(t1)相等.,结果相等。
public class Test {
private int a;
private int b;
public Test(int a, int b){
this.a = a;
this.b = b;
}
public static void main(String args) {
Test t=new Test(2,4);
Test t1=new Test(2,4);
boolean isEqual = t.equals(t1);
System.out.println(isEqual);
}
public boolean equals(Object o) {
if(o instanceof Test){
Test t2 = (Test) o;
return t2.a == this.a && t2.b == this.b;
}
return false;
}
}
总结:如果子类不重写Object.equals()方法,那么两个对象比较的是内存地址;如果子类重写equals()方法,里面可以根据自己的需要来定义,从而避免比较内存地址。。

更多文章:
数据库管理系统和数据库系统分别侧重(数据库,数据库管理系统,数据库系统,这三个分别是什么意思并举个实例)
2026年9月7日 17:00
springmvc的依赖(springMVC的注入方式有哪几种,这与springMVC依赖)
2026年9月7日 14:00
display flex 自动换行(overflow-y:hidden;overflow-x:auto;无效解决方法)
2026年9月7日 11:00
timestamp without time zone(Postgresql中to_date()函数使用问题)
2026年9月7日 09:40






