javasrc.docx
- 文档编号:8879796
- 上传时间:2023-02-02
- 格式:DOCX
- 页数:20
- 大小:18.79KB
javasrc.docx
《javasrc.docx》由会员分享,可在线阅读,更多相关《javasrc.docx(20页珍藏版)》请在冰豆网上搜索。
javasrc
packagecom.example;
/**
*Createdbylpwon12/9/15.
*/
classRectangle{
privatedoublewidth;
privatedoubleheight;
privatedoublearea;
privatedoubleperimeter;
privateStringcolor;
publicRectangle(){
width=1.2;
height=1.1;
color="blue";
}
publicdoublegetWidth(){
returnwidth;
}
publicvoidsetWidth(doublewidth){
this.width=width;
}
publicdoublegetHeight(){
returnheight;
}
publicvoidsetHeight(doubleheight){
this.height=height;
}
publicStringgetColor(){
returncolor;
}
publicvoidsetColor(Stringcolor){
this.color=color;
}
publicdoublegetArea(){
area=width*height;
returnarea;
}
publicdoublegetPerimeter(){
perimeter=(width+height)*2;
returnperimeter;
}
}
packagecom.example;
publicclasstest1_1{
publicstaticvoidmain(String[]args){
Rectanglerectangle1=newRectangle();
System.out.println("ĬÈϵ¥Î»£ºÃ×£¨m£©");
System.out.println("¾ØÐÎĬÈϳõʼֵ£º¿í="+rectangle1.getWidth()+
"£¬¸ß="+rectangle1.getHeight()+
",ÑÕɫΪ"+rectangle1.getColor());
System.out.println("Ãæ»ý£º"+rectangle1.getArea()+"ƽ·½Ã×");
System.out.println("Öܳ¤£º"+rectangle1.getPerimeter()+"Ã×");
System.out.println("");
/*
*ÐÞ¸ÄĬÈÏÖµ
*/
rectangle1.setWidth(2.1);
rectangle1.setHeight(3.2);
rectangle1.setColor("yellow");
System.out.println("Ð޸ĺóµÄÖµ£º¿í="+rectangle1.getWidth()+
"£¬¸ß="+rectangle1.getHeight()+
",ÑÕɫΪ"+rectangle1.getColor());
System.out.println("Ãæ»ý£º"+String.format("%.2f",rectangle1.getArea())+"ƽ·½Ã×");
System.out.println("Öܳ¤£º"+String.format("%.2f",rectangle1.getPerimeter())+"Ã×");
}
}
packagecom.plural;
/**
*Createdbylpwon12/9/15.
*/
classPlural{
doublereal_part;
doubleimaginary_part;
publicPlural(){
this.real_part=1.2;
this.imaginary_part=1.6;
}
publicPlural(doublereal_part,doubleimaginary_part){
this.real_part=real_part;
this.imaginary_part=imaginary_part;
}
publicStringtoString(){
return("("+real_part+"+"+imaginary_part+"i"+")");
}
}
packagecom.plural;
importcom.plural.Plural;
/**
*Createdbylpwon12/9/15.
*/
classPluralOperation{
doublereal_part_addition;
doubleimaginary_part_addition;
doublereal_part_subtraction;
doubleimaginary_part_subtraction;
booleanequality;
publicStringaddPlural(Pluralp1,Pluralp2){
this.real_part_addition=p1.real_part+p2.real_part;
this.imaginary_part_addition=p1.imaginary_part+p2.imaginary_part;
returnString.format("%.2f",real_part_addition)+"+"+String.format("%.2f",imaginary_part_addition)+"i";
}
publicStringsubPlural(Pluralp1,Pluralp2){
this.real_part_subtraction=p1.real_part-p2.real_part;
this.imaginary_part_subtraction=p1.imaginary_part-p2.imaginary_part;
returnString.format("%.2f",real_part_subtraction)+"+"+String.format("%.2f",imaginary_part_subtraction)+"i";
}
publicbooleancmpPlural(Pluralp1,Pluralp2){
equality=(p1.real_part==p2.real_part)&&(p1.imaginary_part==p2.imaginary_part);
returnequality;
}
}
packagecom.plural;
importcom.plural.Plural;
importcom.plural.PluralOperation;
/**
*Createdbylpwon12/9/15.
*/
publicclassTest1_2{
publicstaticvoidmain(String[]args){
Pluralp1=newPlural();
Pluralp2=newPlural(1.4,2.3);
PluralOperationpo=newPluralOperation();
Stringstr1=p1.toString()+"+"+p2.toString()+"="+po.addPlural(p1,p2);
System.out.println(str1);
Stringstr2=p1.toString()+"-"+p2.toString()+"="+po.subPlural(p1,p2);
System.out.println(str2);
if(po.equality)
System.out.println("Á½¸ö¸´ÊýÏàµÈ¡£");
else
System.out.println("Á½¸ö¸´Êý²»ÏàµÈ¡£");
}
}
packagecom.pet;
/**
*Createdbylpwon12/9/15.
*/
classCatextendsPet{
Cat(Stringname){
super(name);
}
@Override
publicStringspeak(){
return"MiaoMiao";
}
}
packagecom.pet;
/**
*Createdbylpwon12/9/15.
*/
classDogextendsPet{
protectedintweight;
publicDog(Strings){
super(s);
}
publicDog(Stringname,intweight,intwith){
with=0;
this.name=name;
this.weight=weight;
}
publicDog(){
}
publicintgetWeight(){
returnweight;
}
publicvoidsetWeight(intweight){
this.weight=weight;
}
@Override
publicStringspeak(){
return"WoofWoof";
}
}
packagecom.pet;
/**
*Createdbylpwon12/9/15.
*/
classPet{
protectedStringname;
publicPet(){//¸¸Àà±ØÐëÒªÓÐ×ÓÀà¿É·ÃÎʵÄÎ޲ι¹Ôì·½·¨
}
publicPet(Stringn){
name=n;
}
publicStringgetName(){
returnname;
}
publicStringmove(){
return"run";
}
publicStringspeak(){
return"";
}
publicStringtoString(){
return"Mypet"+name;
}
}
packagecom.pet;
/**
*Createdbylpwon12/9/15.
*/
publicclassTest{
publicstaticvoidmain(String[]args){
PetmyPet=newPet("George");
PetmyDog=newPet("Spot");
System.out.println(myPet.toString()+"\n"+
"Speak:
"+myPet.speak()+"\n"+
myPet.move()+""+myPet.getName()+"\n");
System.out.println(myDog.toString()+"\n"+
"Speak:
"+myDog.speak()+"\n"+
myDog.move()+""+myDog.getName()+"\n");
Petmypet1=newDog();
System.out.println("PetÀà¶ÔÏómypet1speak:
"+mypet1.speak());
Petmypet2=newCat("Mao");
System.out.println("PetÀà¶ÔÏómypet2speak:
"+mypet2.speak());
}
}
packagesrc;
/**
*Createdbylpwon12/9/15.
*/
importjava.io.*;
importjava.io.BufferedReader;
importjava.io.FileReader;
importjava.io.FileWriter;
importjava.io.IOException;
publicclassSy3_1{
publicstaticvoidmain(String[]args)throwsIOException{
FileReaderin=newFileReader("/home/lpw/AndroidStudioProjects/DisplayingBitmaps/lpwclass/src/main/java/src/Sy3_1.java");
BufferedReaderbin=newBufferedReader(in);
FileWriterout=newFileWriter("Sy3_1.txt",true);
Stringstr;
while((str=bin.readLine())!
=null){
System.out.println(str);
out.write(str+"\n");
}
in.close();
out.close();
}
}
packagesrc;
/**
*Createdbylpwon12/9/15.
*/
importjava.io.DataInputStream;
importjava.io.DataOutputStream;
importjava.io.FileInputStream;
importjava.io.FileNotFoundException;
importjava.io.FileOutputStream;
importjava.io.IOException;
importjava.util.Arrays;
importjava.util.Scanner;
publicclassSy3_2{
staticint[]intArray=newint[20];
staticvoidReadIntFromConsole(){
Scannersc=newScanner(System.in);
System.out.println("Pleaseinput20number:
");
for(inti=0;i<20;i++)
intArray[i]=sc.nextInt();
}
staticvoidWriteToFile(Stringf1,Stringf2)throwsIOException{
FileOutputStreamfout1=newFileOutputStream(f1);
DataOutputStreamdout1=newDataOutputStream(fout1);
FileOutputStreamfout2=newFileOutputStream(f2);
DataOutputStreamdout2=newDataOutputStream(fout2);
inti=0;
for(i=0;i<10;i++){
dout1.writeInt(intArray[i]);
dout2.writeInt(intArray[i+10]);
}
dout1.close();
dout2.close();
fout1.close();
fout2.close();
}
staticvoidReadFromFile(Stringf,intnum)throwsIOException{
FileInputStreamfin=newFileInputStream(f);
DataInputStreamdin=newDataInputStream(fin);
for(inti=0;i intArray[i]=din.readInt(); din.close(); fin.close(); } staticvoidWriteToFile(Stringf,intnum,booleanflag)throwsIOException{ FileOutputStreamfout=newFileOutputStream(f,flag); DataOutputStreamdout=newDataOutputStream(fout); for(inti=0;i dout.writeInt(intArray[i]); dout.close(); fout.close(); } publicstaticvoidmain(String[]args)throwsIOException{ ReadIntFromConsole(); WriteToFile("f1.dat","f2.dat"); ReadFromFile("f1.dat",10); WriteToFile("f2.dat",10,true); ReadFromFile("f2.dat",20); Arrays.sort(intArray); WriteToFile("f2.dat",20,false); ReadFromFile("f2.dat",20); for(inti=0;i<20;i++) System.out.println(intArray[i]+""); } } packagesrc; importjava.io.DataOutputStream; importjava.io.IOException; import.ServerSocket; import.Socket; publicclassSimpleTCPServer{ publicstaticvoidmain(String[]args)throwsIOException{ ServerSocketserver=newServerSocket(8885); Socketsocket=server.accept(); Stringmsg="hello"; DataOutputStreamdos=newDataOutputStream(socket.getOutputStream()); dos.writeUTF(msg); dos.flush(); } } packagesrc; importjava.io.DataInputStream; importjava.io.IOException; import.Socket; import.UnknownHostException; publicclassSimpleTCPClient{ publicstaticvoidmain(String[]args)throwsUnknownHostException,IOException{ Socketclient=newSocket("localhost",8885); DataInputStreamdis=newDataInputStream(client.getInputStream()); Stringecho=dis.readUTF(); System.out.println(echo); } } packagesrc; importjava.io.IOException; import.DatagramPacket; import.DatagramSocket; import.InetSocketAddress; publicclassSimpleUDPClient{ publicstaticvoidmain(String[]args)throwsIOException{ DatagramSocketclient=newDatagramSocket(6666); Stringmsg="udphello"; byte[]data=msg.getBytes(); DatagramPacketpacket=newDatagramPacket(data,data.length,newInetSocketAddress("localhost",8888)); client.send(packet); client.close(); } } packagesrc; importjava.io.IOException; import.DatagramPacket; import.DatagramSocket; importjava.util.Arrays; publicclassSimpleUDPServer{ publicstaticvoidmain(String[]args)throwsIOException{ DatagramSocketserver=newDatagramSocket(8888); byte[]container=newbyte[1024]; DatagramPacketpacket=newDatagramPacket(container,container.length); server.receive(packet); byte[]data=packet.getData(); Sys
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- javasrc