写java代码的参考答案 java编程题目以及答案
关于java的几个选择题
1、java.lang.Math里装的都是和数学计算有关的方法,drawLine是画图的,所以不是.
辉县网站制作公司哪家好,找创新互联公司!从网页设计、网站建设、微信开发、APP开发、响应式网站等网站项目制作,到程序开发,运营维护。创新互联公司自2013年创立以来到现在10年的时间,我们拥有了丰富的建站经验和运维经验,来保证我们的工作的顺利进行。专注于网站建设就选创新互联公司。
2、代码段的文件名要与包含public static void main()方法的类名一致“main() in class B”,所以暗示了文件名为B.java。可能误导你错误的选项是A.class B.class A.java
3、有几个类就会生成几个class文件,class A1,class A2,public class B,所以生成三个文件,且名字和这三个类名一致
根据要求编写java代码,急求,答案正确加100分!求高手!
服务器端:
package com.lqq.service;
import java.io.File;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.List;
import com.lqq.vo.QueryType;
import com.lqq.vo.Student;
public class DealClientRequest implements Runnable
{
private Socket s = null;
private ParserXML p = new ParserXML(new File("students.xml"));
public DealClientRequest(Socket s)
{
this.s = s;
}
@Override
public void run()
{
if(s != null)
{
try
{
ObjectInputStream ois = new ObjectInputStream(s.getInputStream());
ObjectOutputStream oos = new ObjectOutputStream(s.getOutputStream());
QueryType qt = (QueryType) ois.readObject();
if(qt.getQueryType() == 1)//单查
{
System.out.println("执行单查,查询的名字= " + qt.getQueryName());
Student stu = p.getStudent(qt.getQueryName());
oos.writeObject(stu);
}else if(qt.getQueryType() == 2)//全查
{
System.out.println("执行全查");
ListStudent list = p.getAllStudent();
for(int i = 0; i list.size(); i++)
{
Student stu = list.get(i);
oos.writeObject(stu);
}
}
} catch (IOException e)
{
e.printStackTrace();
} catch (ClassNotFoundException e)
{
e.printStackTrace();
}
}
}
}
*************************
package com.lqq.service;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class MainService
{
private ServerSocket ss;
public void startService() throws IOException
{
ss = new ServerSocket(10086);
while(true)
{
System.out.println("服务器在10086等待...");
Socket s = ss.accept();
new Thread(new DealClientRequest(s)).start();
System.out.println("启动处理线程成功");
}
}
}
******************
package com.lqq.service;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
import com.lqq.vo.Student;
public class ParserXML
{
private DocumentBuilderFactory bdf;
private DocumentBuilder db;
private Document dom;
public ParserXML(File file)
{
bdf = DocumentBuilderFactory.newInstance();
try
{
db = bdf.newDocumentBuilder();
dom = db.parse(file);
} catch (Exception e)
{
e.printStackTrace();
}
}
public ListStudent getAllStudent()
{
ListStudent stu = new ArrayListStudent();
Element root = dom.getDocumentElement();
NodeList list = root.getElementsByTagName("stu");
for(int i = 0; i list.getLength(); i++)
{
Element e = (Element) list.item(i);
Student st = new Student();
NodeList names = e.getElementsByTagName("name");
if(names.getLength() == 1)
{
Element name = (Element)names.item(0);
Text text = (Text) name.getFirstChild();
st.setStuName(text.getNodeValue());
}
NodeList sexs = e.getElementsByTagName("sex");
if(sexs.getLength() == 1)
{
Element name = (Element)sexs.item(0);
Text text = (Text) name.getFirstChild();
st.setStuGender(text.getNodeValue());
}
NodeList ages = e.getElementsByTagName("age");
if(ages.getLength() == 1)
{
Element name = (Element)ages.item(0);
Text text = (Text) name.getFirstChild();
st.setStuAge(Integer.parseInt(text.getNodeValue()));
}
NodeList classs = e.getElementsByTagName("class");
if(classs.getLength() == 1)
{
Element name = (Element)classs.item(0);
Text text = (Text) name.getFirstChild();
st.setStuClassName(text.getNodeValue());
}
stu.add(st);
}
return stu;
}
public Student getStudent(String stuName)
{
ListStudent list = this.getAllStudent();
for(int i = 0; i list.size(); i++)
{
Student st = list.get(i);
if(st.getStuName().equals(stuName))
return st;
}
return null;
}
}
*************
package com.lqq.service;
import java.io.IOException;
public class Service
{
public static void main(String[] args)
{
MainService ms = new MainService();
try
{
ms.startService();
} catch (IOException e)
{
e.printStackTrace();
}
}
}
*******
package com.lqq.vo;
import java.io.Serializable;
public class QueryType implements Serializable
{
private static final long serialVersionUID = 8713870836629046060L;
/**
* 1 单查
* 2 全查
*/
private byte queryType;
private String queryName;
public byte getQueryType()
{
return queryType;
}
public void setQueryType(byte queryType)
{
this.queryType = queryType;
}
public String getQueryName()
{
return queryName;
}
public void setQueryName(String string)
{
this.queryName = string;
}
}
************
package com.lqq.vo;
import java.io.Serializable;
public class Student implements Serializable
{
private static final long serialVersionUID = -6087251613589160139L;
private String stuName;
private int stuAge;
private String stuGender;
private String stuClassName;
@Override
public String toString()
{
return "姓名: " + stuName + "\t性别: " + stuGender
+ "\t年龄: " + stuAge + "\t班级: " + stuClassName;
}
public String getStuName()
{
return stuName;
}
public void setStuName(String stuName)
{
this.stuName = stuName;
}
public int getStuAge()
{
return stuAge;
}
public void setStuAge(int stuAge)
{
this.stuAge = stuAge;
}
public String getStuGender()
{
return stuGender;
}
public void setStuGender(String stuGender)
{
this.stuGender = stuGender;
}
public String getStuClassName()
{
return stuClassName;
}
public void setStuClassName(String stuClassName)
{
this.stuClassName = stuClassName;
}
}
*************
?xml version="1.0" encoding="UTF-8"?
stus
stu
name令狐冲/name
age12/age
class华山派/class
sex男/sex
/stu
stu
name东方不败/name
age22/age
class日月神教/class
sex女/sex
/stu
stu
name岳不群/name
age23/age
class华山派/class
sex妖/sex
/stu
stu
name风清扬/name
age88/age
class华山派/class
sex男/sex
/stu
/stus
客户端:
package com.lqq.c;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import com.lqq.vo.QueryType;
import com.lqq.vo.Student;
public class AllQuery implements Runnable
{
private Socket s;
public AllQuery(QueryType qt)
{
try
{
s = new Socket("localhost", 10086);
ObjectOutputStream oos = new ObjectOutputStream(s.getOutputStream());
oos.writeObject(qt);
} catch (UnknownHostException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
}
@Override
public void run()
{
if(s != null)
{
try
{
ObjectInputStream ois = new ObjectInputStream(s.getInputStream());
Student stu = (Student) ois.readObject();
Client c = new Client();
while(stu != null)
{
c.showStudentInfo(stu);
stu = (Student) ois.readObject();
}
} catch (IOException e)
{
e.printStackTrace();
} catch (ClassNotFoundException e)
{
e.printStackTrace();
}
}
}
}
**************
package com.lqq.c;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.Scanner;
import com.lqq.vo.QueryType;
import com.lqq.vo.Student;
public class Client
{
public void showStudentInfo(Student stu)
{
if(stu != null)
{
System.out.println(stu);
}
}
public void showMenu()
{
System.out.println("\t\t菜 单");
System.out.println("\t1.单 查");
System.out.println("\t2.全 查");
System.out.println("\t0.退 出");
}
public String getUserInput()
{
Scanner scan = new Scanner(System.in);
System.out.println("请输入:");
return scan.next();
}
public void requestQuery(String userSelect)
{
if(userSelect != null)
{
if("1".equals(userSelect.trim()))
{
QueryType qt = new QueryType();
qt.setQueryType((byte)1);
Scanner san = new Scanner(System.in);
System.out.println("请输入查询的学生名字:");
String queryStuName = san.next();
qt.setQueryName(queryStuName);
this.executeQeury(qt);
}else if("2".equals(userSelect.trim()))
{
QueryType qt = new QueryType();
qt.setQueryType((byte)2);
this.executeQeury(qt);
}else if("0".equals(userSelect.trim()))
{
System.exit(0);
}
else
{
System.out.println("输入有误");
}
}
}
public void executeQeury(QueryType qt)
{
try
{
Socket s = new Socket("localhost", 10086);
ObjectOutputStream oos = new ObjectOutputStream(s.getOutputStream());
oos.writeObject(qt);
System.out.println("请求发送完毕");
ObjectInputStream ois = new ObjectInputStream(s.getInputStream());
if(qt.getQueryType() == 1)
{
Student stu = (Student) ois.readObject();
showStudentInfo(stu);
}else if(qt.getQueryType() == 2)
{
new Thread(new AllQuery(qt)).start();
}
} catch (IOException e)
{
e.printStackTrace();
} catch (ClassNotFoundException e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
Client client = new Client();
while(true)
{
client.showMenu();
client.requestQuery(client.getUserInput());
}
}
}
**********
package com.lqq.vo;
import java.io.Serializable;
public class QueryType implements Serializable
{
private static final long serialVersionUID = 8713870836629046060L;
/**
* 1 单查
* 2 全查
*/
private byte queryType;
private String queryName;
public byte getQueryType()
{
return queryType;
}
public void setQueryType(byte queryType)
{
this.queryType = queryType;
}
public String getQueryName()
{
return queryName;
}
public void setQueryName(String string)
{
this.queryName = string;
}
}
**********
package com.lqq.vo;
import java.io.Serializable;
public class Student implements Serializable
{
private static final long serialVersionUID = -6087251613589160139L;
private String stuName;
private int stuAge;
private String stuGender;
private String stuClassName;
@Override
public String toString()
{
return "姓名: " + stuName + "\t性别: " + stuGender
+ "\t年龄: " + stuAge + "\t班级: " + stuClassName;
}
public String getStuName()
{
return stuName;
}
public void setStuName(String stuName)
{
this.stuName = stuName;
}
public int getStuAge()
{
return stuAge;
}
public void setStuAge(int stuAge)
{
this.stuAge = stuAge;
}
public String getStuGender()
{
return stuGender;
}
public void setStuGender(String stuGender)
{
this.stuGender = stuGender;
}
public String getStuClassName()
{
return stuClassName;
}
public void setStuClassName(String stuClassName)
{
this.stuClassName = stuClassName;
}
}
时间太紧,没有完善代码,基本功能实现了,代码没有优化,你自己再改改,希望可以帮到你
寻编写java程序的高手 有两个程序需要编写 若能给出真确答案 还是可以加分
第一个题目是不是少了点什么信息,这么写出类来好像没什么意义啊,连个动作都没执行!我这里是按照题目的要求写的代码,一共是2个文件。
package baidu;
/**
* 文件名:Engine.java
* @author
* @version 1.0,创建时间:2012-4-15 下午06:44:18
* @since jdk1.6
* 引擎类,它有一个引擎类型的属性type,
* 其值可能为喷气引擎(whiff)或螺旋桨引擎(airscrew),
*/
public class Engine
{
//一个飞机引擎的私有属性,因为引擎的可能性只有两种,为了限制可能性,所以此处使用枚举类型
private enum type{whiff,airscrew};
}
package baidu;
/**
* 文件名:Airplane.java
* @author
* @version 1.0,创建时间:2012-4-15 下午06:43:30
* @since jdk1.6
* 飞机类,最基础的类。它有1~4个引擎
*/
public class Airplane
{
//飞机的4个引擎,此处分别命名为engine1~4
private Engine engine1;
private Engine engine2;
private Engine engine3;
private Engine engine4;
/**
* 主函数中来执行操作*/
public static void main(String[] args)
{
//定义初始化两个对象喷气式飞机(jet)和螺旋桨飞机(PDAirplane)
Airplane jetAirplane = new Airplane();
Airplane PDAirplane = new Airplane();
}
}
第二题还好,只是有个问题:java里面的接口明明比抽象类好用,扩展性也更好,为什呢不用接口呢?所以我只能说你出了2个题目都是莫名其妙的。下面是我按照题目写的代码,有3个文件,分别代码图形、长方形、三角形:
package baidu;
/**
* 文件名:Polygon.java
* @version 1.0,创建时间:2012-4-15 下午06:54:50
* @since jdk1.6
* 抽象类,代表一个图形,当做是接口处理吧,子类要实现的接口就是getArea()方法。
*/
public abstract class Polygon
{
//该图形的面积
protected double area;
//这里声明方法,不定义
public abstract void getArea();
/**
* 这里弄几个测试用例来测试一下
* @param args
*/
public static void main(String[] args)
{
Polygon changfangPolygon = new Rectangle(10, 10.5);
changfangPolygon.getArea();
Polygon sanjiaopPolygon = new Triangle(4, 5, 3);
sanjiaopPolygon.getArea();
}
}
package baidu;
/**
* 文件名:Rectangle.java
* @version 1.0,创建时间:2012-4-15 下午06:57:27
* @since jdk1.6
* 矩形类,继承了图形类的方法和接口。
*/
public class Rectangle extends Polygon
{
//长方形的长度和宽度
private double length;
private double width;
/**
* 构造方法,用长度和宽度来初始化这个长方形
* @param len 长方形的长度
* @param width 长方形的宽度
*/
public Rectangle(double len,double width)
{
//为了防止输入的数据有负数什么的,要把他们都转成正数
this.length = Math.abs(len);
this.width = Math.abs(width);
}
/**重写父类的方法,计算出面积,输出结果到控制台*/
@Override
public void getArea()
{
/*这里面是使用了父类的area属性,通过protected继承了下来*/
area = this.length*this.width;
System.out.println("长方形【长度:"+this.length+",宽度:"+this.width+"】,面积:"+area);
}
}
package baidu;
/**
* 文件名:Triangle.java
* @version 1.0,创建时间:2012-4-15 下午07:11:09
* @since jdk1.6
* 三角形类,继承了图形类的方法
*/
public class Triangle extends Polygon
{
/**三角形的三边,分别命名为abc好了,随便*/
private double edgeA;
private double edgeB;
private double edgeC;
/**
* 三角形的构造函数,首先验证输入的数据是否合法才初始化
* @param a
* @param b
* @param c
*/
public Triangle(double a,double b,double c)
{
/*临时变量,存储输入的abc三条边*/
double aa ,bb ,cc;
//是否是三角形的判断
boolean isTriangle;
//先保证三条边都是正数,使用取绝对值的方法
aa = Math.abs(a);
bb = Math.abs(b);
cc = Math.abs(c);
//验证两边之和是否大于第三边
isTriangle = (aa+bbcc)(bb+ccaa)(aa+ccbb);
if(!isTriangle)
{
System.out.println("您输入的三边有错误,两边之和大于第三边!");
this.edgeA = 0;
this.edgeB = 0;
this.edgeC = 0;
return ;
}
this.edgeA = aa;
this.edgeB = bb;
this.edgeC = cc;
}
/* (non-Javadoc)
* @see baidu.Polygon#getArea()
* 重载父类的方法,计算出图形的面积,并且输出到控制台
* 三角形的面积计算公式
* (海伦公式)(p=(a+b+c)/2)
S=√[p(p-a)(p-b)(p-c)]
*/
@Override
public void getArea()
{
double p;
p =(this.edgeA+this.edgeB+this.edgeC)/2;
area = Math.sqrt(p*(p-edgeA)*(p-edgeB)*(p-edgeC));
System.out.println("三角形【长度:"+this.edgeA+","+this.edgeB+","+this.edgeC+"】,面积:"+area);
}
}
初学java,基础差,麻烦高手写一下代码,参考一下,谢谢了。如图第二题。
import java.util.Scanner;
class Ca{
private double a;
private double b;
public double add(double a, double b){
return a + b;
}
public double jian(double a, double b){
return a - b;
}
public double chen(double a, double b){
return a * b;
}
public double chu(double a, double b){
if(b == 0){
System.out.println("除数不能为0");
return (Double) null;
}else{
return a / b;
}
}
}
public class Test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("输入第一个数:");
double a = sc.nextDouble();
System.out.println("输入第二个数:");
double b = sc.nextDouble();
System.out.println("两个数的和 : " + new Ca().add(a, b));
System.out.println("两个数的差 : " + new Ca().jian(a, b));
System.out.println("两个数的积 : " + new Ca().chen(a, b));
System.out.println("两个数的商 : " + new Ca().chu(a, b));
}
}
急!求教Java高手!以下是我的Java代码,我在注释中写明了我的想法,和问题,请高手指点!谢谢!
/*我想实现的功能例如将原本D:\test中所有文件,
* 统一命名成类似图片1.jpg,图片2.jpg。。
* 让用户输入想要自动命名的文件夹路径和文件统一命的名称(如图片),
* 然后再让用户输入想要自动命名的文件类型(jgp等)*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
class jfr extends JFrame {
String getpath;
String gettype;
String getname;
JTextField jf1 = new JTextField();// 接收用户输入的文件夹路径
JTextField jf2 = new JTextField();// 接收用户输入的文件夹下的文件类型
JTextField jf3 = new JTextField();// 接收用户输入的文件夹下的文件统一的名称
ImageIcon bu = new ImageIcon("button.gif");// 这个图片我放在了目录下,由于是提问我发送不成图片
JButton sure, cancel;
public jfr() {
Container cp;
cp = getContentPane();
setSize(400, 400);
setLocation(640, 400);
setTitle("文件自动批量命名工具");
setBackground(Color.blue);
setLayout(new GridLayout(4, 2));
setVisible(true);
cp.add(new JLabel("请输入您要批量命名的文件\n所在文件夹路径"));
cp.add(jf1);
cp.add(new JLabel("请输入您要批量命名的文件\n的文件类型,如\"txt\",\"doc\"等"));
cp.add(jf2);
cp.add(new JLabel("请输入您要批量处理的文件\n前缀名:"));
cp.add(jf3);
sure = new JButton("确定", bu);
cancel = new JButton("取消", bu);
cp.add(sure);
cp.add(cancel);
jf1.addActionListener(new actionlisten1());
jf2.addActionListener(new actionlisten1());
sure.setActionCommand("正在批量命名中,请稍后!");
sure.addActionListener(new bu1());
cancel.addActionListener(new bu2());
addWindowListener(new windowlisten());
}// 以下用来实现监听三个文本框的输入,由于字数限制,只写一个
class actionlisten1 implements ActionListener {
public void actionPerformed(ActionEvent e) {
getpath = jf1.getText();
}
}// 监听按钮确定
class bu1 implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.out.println(sure.getActionCommand());
exchange ex = new exchange(gettype, gettype, getname);
boolean an = ex.exchangefiles();
if (an)
System.out.println("文件批量命名完毕,请查阅!");
}// 监听取消按钮
}
class bu2 implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.out.println("已经取消");
}
}
class windowlisten extends WindowAdapter {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}// 这个类我没有定义成类中类,用来实现文件的重命名
class exchange {
String getpath;
String gettype;
String getname;
public exchange(String getp, String gett, String getn) {
getpath = getp;
gettype = gett;
getname = getn;
}// 这个方法用来实现改变文件的名称
public boolean exchangefiles() {
getpath = exchangestring(getpath);
File f1 = new File(getpath);
String files[] = f1.list();
for (int i = 0; i = files.length; i++) {
File f2 = new File(getpath + files[i]);
File f3 = new File(getpath + "\\" + getname + i + "." + gettype);
f3.renameTo(f2);
}
return true;
}// 此方法用来转换用户输入的文件夹路径中'\',将其转换为'\\'
public String exchangestring(String s) {
for (int i = 0; i s.length(); i++) {
if (s.charAt(i) == '\\') {
// 目前我遇到的问题就是这样,无法将路径中的'\'换成'\\'
s = s.substring(0, i) + "\\\\" + s.substring(i);
System.out.println("test!");
}
}
return s;
}
}
}
public class test {
public static void main(String[] args) {
jfr jf = new jfr();
}
}
//自己好好看看。这东西错误好多阿。
//什么错误都有。我就给你改了。另外test是另一个 .java文件中写的。自己看看
//还有就是我只是把你的程序改的不报错误了!
本文名称:写java代码的参考答案 java编程题目以及答案
标题来源:http://scjbc.cn/article/dosdggs.html