
正文
java上机代码 java代码例子讲解
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
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)));
}
}
以上是代码测试
相关问答
Q1: 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;
}
}
Q2: 下列java代码为什么上机输出的是”苹果“而不是“苹果好吃”??
你需要把执行结果传出去,程序的执行是先进先出类型的,而且字符串指向的是一个内存地址,你看起来是s=s+"好吃"的s 与外界的s并不是同一个指针,当你的方法执行完毕后,资源全都会被释放,外面是怎么样还是怎么样,并不会改变。
应该改成public static String mod(String s){return s+"好吃";}
然后外面是s=mod(s);就行了
Q3: 1、编写一个Application程序【java上机作业,要完整代码,急求!!!!!!!!!!】
第一题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 = "你选择java上机代码的是java上机代码:";
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();
}
}
Q4: 一道JAVA上机作业 关于定义时间类的 用netbeans来做 求代码 高手来
//写好了,这是两个文件的代码,你只要复制到就好了。完全按照你的要求做的,你可以测试,书写也规范
public class TestMyTime {
public static void main(String[] args) {
MyTime mytime = new MyTime(12, 30, 20);
System.out.println(mytime);
mytime.advanceTimeOneSecond();
System.out.println(mytime);
int timeInterval = mytime.timeInterval(new MyTime(10, 30, 10));
System.out.println("Second difference:" + timeInterval);
}
}
public class MyTime {
private int hour;
private int minute;
private int second;
public MyTime() {
hour = 0;
minute = 0;
second = 0;
}
public MyTime(int hour, int minute, int second) {
this.hour = hour;
this.minute = minute;
this.second = second;
}
public void advanceTimeOneSecond() {
second++;
}
public int timeInterval(MyTime time) {
return Math.abs((time.hour - this.hour) * 3600
+ (time.minute - this.minute) * 60
+ (time.second - this.second));
}
public String toString() {
return "The time is: " + hour + "-" + minute + "-" + second + "\n";
}
}
java上机代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java代码例子讲解、java上机代码的信息别忘了在本站进行查找喔。






