
正文
java上机代码题 javaoop代码题
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
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上机编程题,求大神帮忙,做了很久,没做出来
第二题
一个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());
}
}
Q2: Java 上机题目
public class BookTest{ public static void main(String[] args){ Book book = new Book("Cook book",150); book.detail(); }} class Book{ private String title; private int pageNum; public Book(String title,int pageNum){ this.title = title; this.judgePage(pageNum); } public String getTitle(){ return this.title; } public void setTitle(String title){ this.title=title; }public int getPageNum(){ return this.pageNum; }public void setPageNum(int pageNum){ this.pageNum=this.judgePage(pageNum);}private int judgePage(int pageNum){ if(pageNum200){ System.out.println("页数["+pageNum+"]不能小于200"); pageNum = 200;
} return pageNum;}public void detail(){ System.out.println("书名:"+this.title+"\n页数:"+this.pageNum);}}
Q3: 求java 上机练习题
1.判断身份证:要么是15位,要么是18位,最后一位可以为字母,并写程序提出其中的年月日。
答:我们可以用正则表达式来定义复杂的字符串格式,(\d{17}[0-9a-zA-Z]|\d{14}[0-9a-zA-Z])可以用来判断是否为合法的15位或18位身份证号码。
因为15位和18位的身份证号码都是从7位到第12位为身份证为日期类型。这样我们可以设计出更精确的正则模式,使身份证号的日期合法,这样我们的正则模式可以进一步将日期部分的正则修改为[12][0-9]{3}[01][0-9][123][0-9],当然可以更精确的设置日期。
在jdk的java.util.Regex包中有实现正则的类,Pattern和Matcher。以下是实现代码:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexTest {
/**
* @param args
*/
public static void main(String[] args) {
// 测试是否为合法的身份证号码
String[] strs = { "130681198712092019", "13068119871209201x",
"13068119871209201", "123456789012345", "12345678901234x",
"1234567890123" };
Pattern p1 = Pattern.compile("(\\d{17}[0-9a-zA-Z]|\\d{14}[0-9a-zA-Z])");
for (int i = 0; i strs.length; i++) {
Matcher matcher = p1.matcher(strs[i]);
System.out.println(strs[i] + ":" + matcher.matches());
}
Pattern p2 = Pattern.compile("\\d{6}(\\d{8}).*"); // 用于提取出生日字符串
Pattern p3 = Pattern.compile("(\\d{4})(\\d{2})(\\d{2})");// 用于将生日字符串进行分解为年月日
for (int i = 0; i strs.length; i++) {
Matcher matcher = p2.matcher(strs[i]);
boolean b = matcher.find();
if (b) {
String s = matcher.group(1);
Matcher matcher2 = p3.matcher(s);
if (matcher2.find()) {
System.out
.println("生日为" + matcher2.group(1) + "年"
+ matcher2.group(2) + "月"
+ matcher2.group(3) + "日");
}
}
}
}
}
1、编写一个程序,将a.txt文件中的单词与b.txt文件中的单词交替合并到c.txt文件中,a.txt文件中的单词用回车符分隔,b.txt文件中用回车或空格进行分隔。
答:
package cn.itcast;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
public class MainClass{
public static void main(String[] args) throws Exception{
FileManager a = new FileManager("a.txt",new char[]{'\n'});
FileManager b = new FileManager("b.txt",new char[]{'\n',' '});
FileWriter c = new FileWriter("c.txt");
String aWord = null;
String bWord = null;
while((aWord = a.nextWord()) !=null ){
c.write(aWord + "\n");
bWord = b.nextWord();
if(bWord != null)
c.write(bWord + "\n");
}
while((bWord = b.nextWord()) != null){
c.write(bWord + "\n");
}
c.close();
}
}
class FileManager{
String[] words = null;
int pos = 0;
public FileManager(String filename,char[] seperators) throws Exception{
File f = new File(filename);
FileReader reader = new FileReader(f);
char[] buf = new char[(int)f.length()];
int len = reader.read(buf);
String results = new String(buf,0,len);
String regex = null;
if(seperators.length 1 ){
regex = "" + seperators[0] + "|" + seperators[1];
}else{
regex = "" + seperators[0];
}
words = results.split(regex);
}
public String nextWord(){
if(pos == words.length)
return null;
return words[pos++];
}
}
1、编写一个程序,将d:\java目录下的所有.java文件复制到d:\jad目录下,并将原来文件的扩展名从.java改为.jad。
(大家正在做上面这道题,网上迟到的朋友也请做做这道题,找工作必须能编写这些简单问题的代码!)
答:listFiles方法接受一个FileFilter对象,这个FileFilter对象就是过虑的策略对象,不同的人提供不同的FileFilter实现,即提供了不同的过滤策略。
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class Jad2Java {
public static void main(String[] args) throws Exception {
File srcDir = new File("java");
if(!(srcDir.exists() srcDir.isDirectory()))
throw new Exception("目录不存在");
File[] files = srcDir.listFiles(
new FilenameFilter(){
public boolean accept(File dir, String name) {
return name.endsWith(".java");
}
}
);
System.out.println(files.length);
File destDir = new File("jad");
if(!destDir.exists()) destDir.mkdir();
for(File f :files){
FileInputStream fis = new FileInputStream(f);
String destFileName = f.getName().replaceAll("\\.java$", ".jad");
FileOutputStream fos = new FileOutputStream(new File(destDir,destFileName));
copy(fis,fos);
fis.close();
fos.close();
}
}
private static void copy(InputStream ips,OutputStream ops) throws Exception{
int len = 0;
byte[] buf = new byte[1024];
while((len = ips.read(buf)) != -1){
ops.write(buf,0,len);
}
}
}
由本题总结的思想及策略模式的解析:
1.
class jad2java{
1. 得到某个目录下的所有的java文件集合
1.1 得到目录 File srcDir = new File("d:\\java");
1.2 得到目录下的所有java文件:File[] files = srcDir.listFiles(new MyFileFilter());
1.3 只想得到.java的文件: class MyFileFilter implememyts FileFilter{
public boolean accept(File pathname){
return pathname.getName().endsWith(".java")
}
}
2.将每个文件复制到另外一个目录,并改扩展名
2.1 得到目标目录,如果目标目录不存在,则创建之
2.2 根据源文件名得到目标文件名,注意要用正则表达式,注意.的转义。
2.3 根据表示目录的File和目标文件名的字符串,得到表示目标文件的File。
//要在硬盘中准确地创建出一个文件,需要知道文件名和文件的目录。
2.4 将源文件的流拷贝成目标文件流,拷贝方法独立成为一个方法,方法的参数采用抽象流的形式。
//方法接受的参数类型尽量面向父类,越抽象越好,这样适应面更宽广。
}
分析listFiles方法内部的策略模式实现原理
File[] listFiles(FileFilter filter){
File[] files = listFiles();
//Arraylist acceptedFilesList = new ArrayList();
File[] acceptedFiles = new File[files.length];
int pos = 0;
for(File file: files){
boolean accepted = filter.accept(file);
if(accepted){
//acceptedFilesList.add(file);
acceptedFiles[pos++] = file;
}
}
Arrays.copyOf(acceptedFiles,pos);
//return (File[])accpetedFilesList.toArray();
}
1、编写一个截取字符串的函数,输入为一个字符串和字节数,输出为按字节截取的字符串,但要保证汉字不被截取半个,如“我ABC”,4,应该截取“我AB”,输入“我ABC汉DEF”,6,应该输出“我ABC”,而不是“我ABC+汉的半个”。
答:
首先要了解中文字符有多种编码及各种编码的特征。
假设n为要截取的字节数。
public static void main(String[] args) throws Exception{
String str = "我a爱中华abc我爱传智def';
String str = "我ABC汉";
int num = trimGBK(str.getBytes("GBK"),5);
System.out.println(str.substring(0,num) );
}
public static int trimGBK(byte[] buf,int n){
int num = 0;
boolean bChineseFirstHalf = false;
for(int i=0;in;i++)
{
if(buf[i]0 !bChineseFirstHalf){
bChineseFirstHalf = true;
}else{
num++;
bChineseFirstHalf = false;
}
}
return num;
}
1、有一个字符串,其中包含中文字符、英文字符和数字字符,请统计和打印出各个字符的个数。
答:哈哈,其实包含中文字符、英文字符、数字字符原来是出题者放的烟雾弹。
String content = “中国aadf的111萨bbb菲的zz萨菲”;
HashMap map = new HashMap();
for(int i=0;icontent.length;i++)
{
char c = content.charAt(i);
Integer num = map.get(c);
if(num == null)
num = 1;
else
num = num + 1;
map.put(c,num);
}
for(Map.EntrySet entry : map)
{
system.out.println(entry.getkey() + “:” + entry.getValue());
}
估计是当初面试的那个学员表述不清楚,问题很可能是:
如果一串字符如"aaaabbc中国1512"要分别统计英文字符的数量,中文字符的数量,和数字字符的数量,假设字符中没有中文字符、英文字符、数字字符之外的其他特殊字符。
int engishCount;
int chineseCount;
int digitCount;
for(int i=0;istr.length;i++)
{
char ch = str.charAt(i);
if(ch=’0’ ch=’9’)
{
digitCount++
}
else if((ch=’a’ ch=’z’) || (ch=’A’ ch=’Z’))
{
engishCount++;
}
else
{
chineseCount++;
}
}
System.out.println(……………);
Q4: 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();
}
/**
* 非递归中序遍历
* 栈内保存将要访问java上机代码题的元素
*/
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();
}
/**
* 非递归后序遍历
* 当上一个访问java上机代码题的结点是右孩子或者当前结点没有右孩子则访问当前结点
*/
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("深度为java上机代码题:" + 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上机代码题和javaoop代码题的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。








