1、java.lang.Math里裝的都是和數(shù)學計算有關(guān)的方法,drawLine是畫圖的,所以不是.
輝縣網(wǎng)站制作公司哪家好,找創(chuàng)新互聯(lián)公司!從網(wǎng)頁設計、網(wǎng)站建設、微信開發(fā)、APP開發(fā)、響應式網(wǎng)站等網(wǎng)站項目制作,到程序開發(fā),運營維護。創(chuàng)新互聯(lián)公司自2013年創(chuàng)立以來到現(xiàn)在10年的時間,我們擁有了豐富的建站經(jīng)驗和運維經(jīng)驗,來保證我們的工作的順利進行。專注于網(wǎng)站建設就選創(chuàng)新互聯(lián)公司。
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,所以生成三個文件,且名字和這三個類名一致
服務器端:
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("執(zhí)行單查,查詢的名字= " + qt.getQueryName());
Student stu = p.getStudent(qt.getQueryName());
oos.writeObject(stu);
}else if(qt.getQueryType() == 2)//全查
{
System.out.println("執(zhí)行全查");
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("請求發(fā)送完畢");
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;
}
}
時間太緊,沒有完善代碼,基本功能實現(xiàn)了,代碼沒有優(yōu)化,你自己再改改,希望可以幫到你
第一個題目是不是少了點什么信息,這么寫出類來好像沒什么意義啊,連個動作都沒執(zhí)行!我這里是按照題目的要求寫的代碼,一共是2個文件。
package?baidu;
/**
*?文件名:Engine.java
*?@author
*?@version?1.0,創(chuàng)建時間: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,創(chuàng)建時間: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;
/**
?*?主函數(shù)中來執(zhí)行操作*/
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,創(chuàng)建時間:2012-4-15?下午06:54:50
*?@since?jdk1.6
*?抽象類,代表一個圖形,當做是接口處理吧,子類要實現(xiàn)的接口就是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,創(chuàng)建時間:2012-4-15?下午06:57:27
*?@since?jdk1.6
*?矩形類,繼承了圖形類的方法和接口。
*/
public?class?Rectangle?extends?Polygon
{
//長方形的長度和寬度
private?double?length;
private?double?width;
/**
?*?構(gòu)造方法,用長度和寬度來初始化這個長方形
?*?@param?len?長方形的長度
?*?@param?width?長方形的寬度
?*/
public?Rectangle(double?len,double?width)
{
//為了防止輸入的數(shù)據(jù)有負數(shù)什么的,要把他們都轉(zhuǎn)成正數(shù)
this.length?=?Math.abs(len);
this.width?=?Math.abs(width);
}
/**重寫父類的方法,計算出面積,輸出結(jié)果到控制臺*/
@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,創(chuàng)建時間:2012-4-15?下午07:11:09
*?@since?jdk1.6
*?三角形類,繼承了圖形類的方法
*/
public?class?Triangle?extends?Polygon
{
/**三角形的三邊,分別命名為abc好了,隨便*/
private?double?edgeA;
private?double?edgeB;
private?double?edgeC;
/**
?*?三角形的構(gòu)造函數(shù),首先驗證輸入的數(shù)據(jù)是否合法才初始化
?*?@param?a
?*?@param?b
?*?@param?c
?*/
public?Triangle(double?a,double?b,double?c)
{
/*臨時變量,存儲輸入的abc三條邊*/
double?aa?,bb?,cc;
//是否是三角形的判斷
boolean?isTriangle;
//先保證三條邊都是正數(shù),使用取絕對值的方法
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);
}
}
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("除數(shù)不能為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("輸入第一個數(shù):");
double?a?=?sc.nextDouble();
System.out.println("輸入第二個數(shù):");
double?b?=?sc.nextDouble();
System.out.println("兩個數(shù)的和?:?"?+?new?Ca().add(a,?b));
System.out.println("兩個數(shù)的差?:?"?+?new?Ca().jian(a,?b));
System.out.println("兩個數(shù)的積?:?"?+?new?Ca().chen(a,?b));
System.out.println("兩個數(shù)的商?:?"?+?new?Ca().chu(a,?b));
}
}
/*我想實現(xiàn)的功能例如將原本D:\test中所有文件,
* 統(tǒng)一命名成類似圖片1.jpg,圖片2.jpg。。
* 讓用戶輸入想要自動命名的文件夾路徑和文件統(tǒng)一命的名稱(如圖片),
* 然后再讓用戶輸入想要自動命名的文件類型(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();// 接收用戶輸入的文件夾下的文件統(tǒng)一的名稱
ImageIcon bu = new ImageIcon("button.gif");// 這個圖片我放在了目錄下,由于是提問我發(fā)送不成圖片
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());
}// 以下用來實現(xiàn)監(jiān)聽三個文本框的輸入,由于字數(shù)限制,只寫一個
class actionlisten1 implements ActionListener {
public void actionPerformed(ActionEvent e) {
getpath = jf1.getText();
}
}// 監(jiān)聽按鈕確定
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("文件批量命名完畢,請查閱!");
}// 監(jiān)聽取消按鈕
}
class bu2 implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.out.println("已經(jīng)取消");
}
}
class windowlisten extends WindowAdapter {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}// 這個類我沒有定義成類中類,用來實現(xiàn)文件的重命名
class exchange {
String getpath;
String gettype;
String getname;
public exchange(String getp, String gett, String getn) {
getpath = getp;
gettype = gett;
getname = getn;
}// 這個方法用來實現(xiàn)改變文件的名稱
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;
}// 此方法用來轉(zhuǎn)換用戶輸入的文件夾路徑中'\',將其轉(zhuǎn)換為'\\'
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文件中寫的。自己看看
//還有就是我只是把你的程序改的不報錯誤了!