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编程作业 多线程Web服务器 小女子跪求答案
1、创建一个动物集合,插入动物园中有的几种动物(请给出10种)
2、一次性输出内容
3、使用iterator遍历集合中所有内容
4、将集合内容转存储于一个数字内,并在数组中进行排序
只列举了2种动物,自己再添加
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
/**
*
* @author Administrator
*/
interface Animal {
public String toString();
}
class Cat implements Animal,Comparable<Animal> {
public String name;
public Cat(){
super();
name = "猫";
}
public String toString(){
return name;
}
@Override
public int compareTo(Animal o) {
if(this.toString().compareTo(o.toString())==-1){
return -1;
}else if(this.toString().compareTo(o.toString())==0){
return 0;
}else
return 1;
}
}
class Dog implements Animal,Comparable<Animal> {
public String name;
public Dog(){
super();
name="狗";
}
public String toString(){
return name;
}
@Override
public int compareTo(Animal o) {
if(this.toString().compareTo(o.toString())==-1){
return -1;
}else if(this.toString().compareTo(o.toString())==0){
return 0;
}else
return 1;
}
}
public class Demo8 {
public static void main(String[] args){
// 创建一个动物集合,插入动物园中有的几种动物
Collection<Animal> col = new ArrayList<Animal>();
col.add(new Cat());
col.add(new Dog());
col.add(new Cat());
// 一次性输出内容
System.out.println(Arrays.toString(col.toArray()));
//使用iterator遍历集合中所有内容
//并将集合内容转存储于一个数组内
Iterator<Animal> it = col.iterator();
int n = 0;
Animal[] an = new Animal[3];
while(it.hasNext()){
Animal temp = (Animal)it.next();
System.out.println("使用iterator遍历集合中所有内容:"+temp);
an[n++]=temp;
}
//并在数组中进行排序
Arrays.sort(an);
System.out.println(Arrays.toString(an));
}
❸ java编程题 答案完整追加50分
public abstract class DepositBook {
protected String account;// 表示存折账号
protected double money;// 表示存折余额
protected String name;// 表示账户名
public DepositBook(String account, String name, double money) {
this.account = account;
this.name = name;
this.money = money;
}
public abstract void depositMoney(double addMoney); // 代表存款行为
public abstract double drawMoney(double moneyCount); // 代表取款行为
public void printInfo() {
System.out.println("姓名:" + name);
System.out.println("账号:" + account);
System.out.println("余额:" + money);
}
}
====================
public class DemandDepositBook extends DepositBook {
public DemandDepositBook(String account, String name, double money) {
super(account, name, money);
}
@Override
public void depositMoney(double addMoney) {
money += addMoney;
}
@Override
public double drawMoney(double moneyCount) {
if (money > moneyCount) {
money -= moneyCount;
return moneyCount;
}
money = 0;
return money;
}
}
❹ java编程作业,求答案··
class Student
{
private String name;
private String sex;
private int age;
private String grade;
public Student()
{
this("","",0,"");
}
public Student(String name,String sex,int age,String grade)
{
this.name=name;
this.sex=sex;
this.age=age;
this.grade=grade;
}
public void setName(String name)
{this.name=name;}
public void setSex(String sex)
{this.sex=sex;}
public void setA(int age)
{this.age=age;}
public void setGrade(String grade)
{this.grade=grade;}
public String toString()
{
return name+","+sex+","+age+","+grade;
}
}
另外一类:
public class X
{
public static void main(String arg[])
{
Student student=new Student();
System.out.println(student);
}
}
❺ java编程题求答案
原来写的一个socket 例子,你可以做参考
package com.test;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Main {
private static final int PORT = 3600;
private List<Socket> mList = new ArrayList<Socket>();
private ServerSocket server = null;
private ExecutorService mExecutorService = null; // thread pool
public static void main(String[] args) {
new Main();
}
public Main() {
try {
server = new ServerSocket(PORT);
mExecutorService = Executors.newCachedThreadPool(); // create a
// thread pool
System.out.print("server start ...");
Socket client = null;
while (true) {
client = server.accept();
mList.add(client);
mExecutorService.execute(new Service(client)); // start a new
// thread to
// handle the
// connection
}
} catch (Exception e) {
e.printStackTrace();
}
}
class Service implements Runnable {
private Socket socket;
private BufferedReader in = null;
private String msg = "";
public Service(Socket socket) {
this.socket = socket;
try {
in = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
msg = "user" + this.socket.getInetAddress() + "come toal:"
+ mList.size();
this.sendmsg();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void run() {
// TODO Auto-generated method stub
try {
while (true) {
if ((msg = in.readLine()) != null) {
if (msg.equals("exit")) {
System.out.println("ssssssss");
mList.remove(socket);
in.close();
msg = "user:" + socket.getInetAddress()
+ "exit total:" + mList.size();
socket.close();
this.sendmsg();
break;
} else {
msg = socket.getInetAddress() + ":" + msg;
this.sendmsg();
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void sendmsg() {
System.out.println(msg);
int num = mList.size();
for (int index = 0; index < num; index++) {
Socket mSocket = mList.get(index);
PrintWriter pout = null;
try {
pout = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(mSocket.getOutputStream())),
true);
pout.println(msg);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
❻ 求JAVA语言程序设计作业答案
3、C;4、A;6、A;7、A;8、D;12、A;13、B;14、B;17、A;18、D;19、D;20、A;
4题:A很明显是错误的,java程序在运行前需要编译成字节码文件,才能运行。
14题:在Java语言中,标识符是以字母、下划线或美元符开头,由字母、数字、下划线或美元符组成的字符串。标识符区分大小写,长度没有限制。除以上所列几项之外,标识符中不能含有其他符号,也不允许插入空格。
17题:向main方法传入的是三个参数接收后args[]={"aaa","bb","c"}
int k1=args.length;//得到数组元素个数,为3
int k2=args[1].length();//得到数组中下标为1的元素字符数(即第二个元素)
18题:String s1="AbcD"; String s2=s1.toLowerCase(); 作用是把字符串全部转为小写,所以选D
19题:函数重载定义 1:保持相同的功能,并且有相同的函数名
2、重载方式为:返回值类型不同,形参个数不同同,形参类型不同。
在满足一的前提下,二中三个条件任意一个,或其中多个的任意组合都是重载
20题:BB继承了AA并且重写了Show()方法。父类AA实例化了a,所以a.Show()调用的是父类中的Show方法,显示:我喜欢Java!子类BB实例化了b,所以b.Show()调用子类BB中的Show方法,显示:我喜欢C++!
❼ 跪求Java编程题答案,我一点都不会~~~(>_<)~
(1)
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Please enter...");
String name = s.next();
String studentId = s.next();
String specialty = s.next();
System.out.println("姓名:"+name+",学号:"+studentId+" ,专业: "+specialty);
}
}
--------------------------------------
--------------------------------------
(2)
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Please enter...");
int n = s.nextInt();
double sum = 0;
for(int i=1;i<=n;i++){
if(i%2==1){
sum += 1.00/(2*i-1);
}else{
sum -= 1.00/(2*i-1);
}
}
System.out.println("PI = "+4*sum);
}
}
❽ java编程作业,求答案
import java.awt.*;
import java.awt.event.*;
public class TestFrame {
public static void main(String[] args) {
Frame frame = new Frame("姓名:xxx");
frame.setLayout(new BorderLayout());
Panel panel1 = new Panel();
Panel panel2 = new Panel();
TextField tf = new TextField(20);
panel1.add(tf);
panel2.setLayout(new GridLayout(4,4));
panel2.add(new Button("0"));
panel2.add(new Button("1"));
panel2.add(new Button("2"));
panel2.add(new Button("3"));
panel2.add(new Button("4"));
panel2.add(new Button("5"));
panel2.add(new Button("6"));
panel2.add(new Button("7"));
panel2.add(new Button("8"));
panel2.add(new Button("9"));
panel2.add(new Button("A"));
panel2.add(new Button("B"));
panel2.add(new Button("C"));
panel2.add(new Button("D"));
panel2.add(new Button("E"));
panel2.add(new Button("F"));
frame.add(panel1, BorderLayout.NORTH);
frame.add(panel2, BorderLayout.SOUTH);
frame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.pack();
frame.setVisible(true);
}
}