java高级网络编程期末考试答案(网络编程期末试卷)
JAVA 题 求答案
public?class?Test2?{
/**
?*?@param?args
?*/
public?static?void?main(String[]?args)?{
UseCompute?use=new?UseCompute();
use.useCom(new?Plus(),?1,?2);//+
use.useCom(new?Minus(),?2,?2);//-
use.useCom(new?Time(),?3,?2);//*
use.useCom(new?Divide(),?4,?2);///
}
}
class?UseCompute{
public?void?useCom(Compute?com,?int?one,?int?two){
System.out.println("运算结果是:"+com.computer(one,?two));?
};
}
interface?Compute{
abstract?int?computer(int?m,int?n);
}
class?Plus?implements?Compute{
@Override
public?int?computer(int?m,?int?n)?{
//+方法
return?m+n;
}
}
class?Minus?implements?Compute{
@Override
public?int?computer(int?m,?int?n)?{
//-方法
return?m-n;
}
}
class?Time?implements?Compute{
@Override
public?int?computer(int?m,?int?n)?{
//*方法
return?m*n;
}
}
class?Divide?implements?Compute{
@Override
public?int?computer(int?m,?int?n)?{
///方法
return?m/n;
}
}
又是作业吧?
java练习题需要答案
1、d 面向对象的三大特征是 封装、继承、多态
2、d 不能以数字开头 只能一下划线,$,字母 作为开头
3、a 不做解释
4、b
5、c 子类只能有一个父类,父类可以有多个子类,子类可以实现多个接口
6、b 因为case2没加break;

求一个java编程题的答案
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
class Student {
private String id;
private String name;
private boolean isMale;
private Date birth;
public Student() {
super();
}
public Student(String id, String name, boolean isMale, Date birth) {
super();
this.id = id;
this.name = name;
this.isMale = isMale;
this.birth = birth;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isMale() {
return isMale;
}
public void setMale(boolean isMale) {
this.isMale = isMale;
}
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", isMale=" + isMale + ", birth=" + birth + "]";
}
}
public class StudentTest {
public static void main(String[] args) throws ParseException {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-mm-dd");
Student stu = new Student("00001", "Tom", true, dateFormat.parse("2000-01-01"));
System.out.println(stu);
}
}
求Java答案
1 基本数据类型(就是int那些) 应用数据类型(就是对象)
2 标识符(就是名字吧)
3 类名.方法名 (静态的不需要实例对象就能访问)
4 package 第一个
5 InputStream Reader
1 D
2 D
3 B
4 A
5 D
6 C
7 D
8 B
9 C
10 C
JAVA网络编程基础试题!300分!请一定要帮我做一下,两个解答题。一个编程题,重点是编程题,谢谢了!!
2.百度百科搜索很详细
3.附件中的代码仅供参考
TcpSocketServerDemo.java
package?yaoshun.InetAddress;
import?java.io.DataInputStream;
import?java.io.DataOutputStream;
import?java.io.IOException;
import?java.net.ServerSocket;
import?java.net.Socket;
import?java.util.ArrayList;
import?java.util.List;
import?java.util.Random;
/*
?*?客户端:TCP服务器端
?*/
public?class?TcpSocketServerDemo?{
private?ServerSocket?serverSocket;//?服务器连接
private?int?port;?//?服务器监听端口
private?ListSocket?sockets;
private?int?ranNumber;
/**
?*?服务器结束的标志
?*/
public?boolean?ISEND;
public?TcpSocketServerDemo(int?port)?{
this.port?=?port;
serverSocket?=?null;
sockets?=?new?ArrayListSocket();
ranNumber?=?new?Random().nextInt(50);
}
/*
?*?启动服务器
?*/
public?void?starServer()?{
try?{
//?第一步:建立服务器连接(绑定监听端口)
serverSocket?=?new?ServerSocket(port);
System.out.println("姚舜的服务器已经启动...");
Socket?socket;//?客户端连接(如果建立连接时,本机网络不容或服务器程序未启动则会抛出异常)
ServerThread?serverThread;
Thread?t2;
while?(!ISEND)?{
//?第二步:监听端口(获取客户端连接)
socket?=?serverSocket.accept();
if?(ISEND)?{
while?(!socket.isClosed())?{
socket.close();
Thread.sleep(100);
}
}?else?{
sockets.add(socket);
serverThread?=?new?ServerThread(this,?socket);
t2?=?new?Thread(serverThread);
t2.start();
}
}
for?(Socket?s?:?sockets)?{
s.close();
}
serverSocket.close();
}?catch?(Exception?e)?{
e.printStackTrace();
}
}
public?static?void?main(String[]?args)?{
TcpSocketServerDemo?tDemo?=?new?TcpSocketServerDemo(1000);
tDemo.starServer();
}
/**
?*?校验
?*?
?*?@param?i
?*?@return
?*/
public?int?check(int?i)?{
if?(i??ranNumber)?{
return?-1;
}?else?if?(i??ranNumber)?{
return?1;
}
return?0;
}
}
/**
?*?服务端处理线程
?*
?*/
class?ServerThread?implements?Runnable?{
private?TcpSocketServerDemo?tDemo;
private?Socket?socket;
private?DataInputStream?dis;//?输入流(读取数据)
private?DataOutputStream?dos;//?输出流(发送数据)
public?ServerThread(TcpSocketServerDemo?tDemo,?Socket?socket)?{
super();
this.tDemo?=?tDemo;
this.socket?=?socket;
try?{
dis?=?new?DataInputStream(socket.getInputStream());
dos?=?new?DataOutputStream(socket.getOutputStream());
}?catch?(IOException?e)?{
e.printStackTrace();
}
}
public?void?run()?{
String?message?=?"";
int?i;
int?index?=?0;
int?result?=?0;
while?(!message.equals("Quit")??index??5)?{
try?{
message?=?dis.readUTF();
}?catch?(IOException?e)?{
e.printStackTrace();
}
try?{
i?=?Integer.parseInt(message);
result?=?tDemo.check(i);
if?(result?==?1)?{
dos.writeUTF("数字大了");
}?else?if?(result?==?-1)?{
dos.writeUTF("数字小了");
}
if?(result?==?0)?{
dos.writeUTF("猜中了");
break;
}
}?catch?(NumberFormatException?|?IOException?e)?{
continue;
}
index++;
}
try?{
dis.close();
dos.close();
socket.close();
}?catch?(IOException?e)?{
e.printStackTrace();
}
}
}
TcpSocketClientDemo.java
package?yaoshun.InetAddress;
import?java.io.DataInputStream;
import?java.io.DataOutputStream;
import?java.io.IOException;
import?java.net.Socket;
import?java.net.UnknownHostException;
import?java.util.Scanner;
/*
?*?演示类:TCP客户端
?*/
public?class?TcpSocketClientDemo?{
private?Socket?socket;//?客户端连接(如果建立连接时,本机网络不容或服务器程序未启动则会抛出异常)
private?DataInputStream?dis;//?输入流(读取数据)
private?DataOutputStream?dos;//?输出流(发送数据)
//?private?String?serverIP;//?服务器IP地址
//?private?int?serverPort;//?服务器监听端口
private?String?sendData;//?发送的数据
public?TcpSocketClientDemo(String?serverIP,?int?serverPort)?throws?UnknownHostException,?IOException?{
//?this.serverIP?=?serverIP;
//?this.serverPort?=?serverPort;
socket?=?new?Socket(serverIP,?serverPort);
dis?=?new?DataInputStream(socket.getInputStream());
dos?=?new?DataOutputStream(socket.getOutputStream());
sendData?=?null;
startClient();
}
public?void?startClient()?{
try?{
//?第一步:创建客户端连接
System.out.println("===我的客户端界面===");
//?第二步:客户端向服务器发送数据
int?i?=?0;
Scanner?input?=?new?Scanner(System.in);
while?(i??5)?{
System.out.println("请输入你的内容(客户端的):");
sendData?=?input.nextLine();
if?(sendData.equals("quit"))?{
break;
}
dos.writeUTF(sendData);
sendData?=?dis.readUTF();
System.out.println("接收到服务器内容:"?+?sendData);
if?(i?==?5)?{
System.out.println("超过允许次数,请重新连接服务器.");
}
if?(sendData.equals("猜中了"))?{
i?=?5;
}
i++;
}
input.close();
}?catch?(Exception?e)?{
e.printStackTrace();
}?finally?{
//?第四步:关闭输入流、输出流和客户端连接
try?{
dis.close();
dos.close();
socket.close();
}?catch?(Exception?e)?{
e.printStackTrace();
}
}
}
public?static?void?main(String[]?args)?{
try?{
new?TcpSocketClientDemo("127.0.0.1",?1000);
}?catch?(IOException?e)?{
e.printStackTrace();
}
}
}