Northwest Information Engineering College level 10 Java Test no standard answer __java

Source: Internet
Author: User
Tags lowercase stub

The first question: graphical interface, input text address, display content in textfiled, click on the conversion button, turn uppercase letters to lowercase letters, lowercase letters into capital letters and output in another textfiled

Import Java.io.File;
Import Java.io.FileInputStream;
Import Org.eclipse.swt.widgets.Display;
Import Org.eclipse.swt.widgets.Shell;
Import Org.eclipse.swt.widgets.Label;

Import Org.eclipse.swt.SWT;

Import Org.eclipse.swt.widgets.Text;

Import Org.eclipse.swt.widgets.Button;

Import Org.eclipse.swt.events.SelectionAdapter;

Import org.eclipse.swt.events.SelectionEvent;

Import Org.eclipse.swt.events.MouseAdapter;

 

 

Import org.eclipse.swt.events.MouseEvent;

public class Task1 {protected Shell shell;

Private Text Textpath;

Private Text Textrecord;

 

Private Text Texttran;

/** * Launch the application.

* @param args */public static void main (string[] args) {try {Task1 window = new Task1 ();

window.open ();

catch (Exception e) {e.printstacktrace ();

}/** * Open the window.

* * public void Open () {Display display = Display.getdefault ();

Createcontents ();

Shell.open ();

Shell.layout (); while (!shell.isdisposed ()) {if (!display.readanddispatch ()) {

Display.sleep ();

}}/** * Create contents of the window.

*/protected void createcontents () {shell = new shell ();

Shell.setsize (450, 300);

 

Shell.settext ("\u6587\u672c\u7f16\u8f91\u5668"); Label label = new label (Shell, SWT.

NONE);

Label.setbounds (32, 36, 63, 13);

 

Label.settext ("\u6587\u4ef6\u8def\u5f84\uff1a"); Textpath = new Text (Shell, SWT.

BORDER);

 

Textpath.setbounds (121, 36, 266, 19); Button Buttonread = New button (Shell, SWT.

NONE); Buttonread.addmouselistener (New Mouseadapter () {@Override public void mouseUp (MouseEvent e) {String path = TEXTPATH.G

 

Ettext ();

BYTE bytes[]= new byte[1024];

int index=0;

try{File File = new file (path);

if (!file.exists ()) {Textrecord.settext ("text does not exist");

} else{FileInputStream is = new FileInputStream (file);

while (index = Is.read (bytes,0,1024)) >0) {String str = new String (bytes,0,index);

Textrecord.settext (str);

\ Exception ee {ee.printstacktrace ()}

}

}); Buttonread.addselectiOnlistener (New Selectionadapter () {@Override public void widgetselected (Selectionevent e) {}});

Buttonread.setbounds (71, 71, 82, 23);

 

Buttonread.settext ("\u8bfb\u5165"); Button Buttontrans = New button (Shell, SWT.

NONE); Buttontrans.addmouselistener (New Mouseadapter () {@Override public void mouseUp (MouseEvent e) {try{if (textrecord.get

Text () ==null) {texttran.settext ("text field is blank");

} else{String Text=null;

byte bytes[] = new byte[1024];

Char chs[]= new char[1024];

bytes = Textrecord.gettext (). toString (). GetBytes ();

CHS = Textrecord.gettextchars ();

int len = Textrecord.getcharcount ();

int index = 0;

 

while (Index<len) {if (chs[index]<=122&&chs[index]>=97) {Chs[index] = (char) ((int) chs[index]-32);

' Else if (chs[index]>=65&&chs[index]<=90) {Chs[index] = (char) ((int) chs[index]+32);}

 

index++;

} texttran.settext (String.valueof (CHS));

}}catch (Exception ee) {ee.printstacktrace ();}

}

}); Buttontrans.addselectionlistener (NEW Selectionadapter () {@Override public void widgetselected (Selectionevent e) {}});

Buttontrans.setbounds (248, 71, 82, 23);

 

Buttontrans.settext ("\u8f6c\u6362"); Textrecord = new Text (Shell, SWT.

BORDER);

 

Textrecord.setbounds (34, 100, 166, 137); Texttran = new Text (Shell, SWT.

BORDER);

 

Texttran.setbounds (238, 100, 166, 137); }

}

Second, the socket, the client input an arithmetic: 2+3=, server-side calculation results, return to the client

Client import java.io.*;

Import java.net.*; public class Task2client {/** * @param args */public static void main (string[] args) {//TODO auto-generated Me

Thod stub try{System.out.println ("Start connecting server");

Socket socket = new socket ("localhost", 8888);

System.out.println ("Already connected server");

OutputStream os= Socket.getoutputstream ();

 

InputStream is = Socket.getinputstream ();

byte bytes[] = new BYTE[3];

InputStreamReader ISR = new InputStreamReader (system.in);

Char chs[] = new CHAR[50];

int index = 0;

 

System.in.read (bytes);

Os.write (bytes);

 

SYSTEM.OUT.PRINTLN ("already sent");

InputStreamReader ISR = new InputStreamReader (IS);

Index=isr.read (CHS);

 

System.out.println (New String (Chs,0,index));

}catch (Exception e) {e.printstacktrace ();}

}//server-side import java.io.*;

Import java.net.*; public class Task2server {/** * @param args */public static void main (string[] args) {//TODO auto-generated Me
Thod stub try{ServerSocket ss = new ServerSocket (8888);
SYSTEM.OUT.PRINTLN ("Server Open");

Socket socket = ss.accept ();

SYSTEM.OUT.PRINTLN ("Find client");

InputStream is = Socket.getinputstream ();

InputStreamReader ISR = new InputStreamReader (IS);

OutputStream os= Socket.getoutputstream ();

PrintWriter pw = new PrintWriter (OS);

Char chs[] = new CHAR[3];

int index = 0;

 

float r=0;

while (Isr.read (CHS)!=-1) {int a = Integer.parseint (string.valueof (chs[0)));

 

int b = Integer.parseint (string.valueof (chs[2]));

 

if (chs[1]== ' + ') {R =a+b;

Pw.write (string.valueof (R));

 

Pw.flush ();

System.out.println (R);

 

else if (chs[1]== '-') {R =a-b;

Pw.write (string.valueof (R));

Pw.flush ();

System.out.println (R);

 

 

else if (chs[1]== ' * ') {R =a*b;

Pw.write (string.valueof (R));

Pw.flush ();

System.out.println (R);

 

else if (chs[1]== '/') {R =a/b;

Pw.write (string.valueof (R));

Pw.flush ();

 

System.out.println (CHS);

System.out.println (R);

 

else {pw.write ("Error");

Pw.flush ();

System.out.println (CHS); System.out. println ("Error");

}/*socket ssocket =new Socket ("localhost", 8889);

OutputStream os =ssocket.getoutputstream ();

OutputStreamWriter osw= new OutputStreamWriter (OS); * * Isr.close ();

Is.close ();

Socket.close ();

Ss.close ();

 

}catch (Exception e) {e.printstacktrace ();}

  }

 

}


The third question: The database operation, calculates the average score

Import java.sql.*; public class Task3 {/** * @param args */public static void main (string[] args) {//TODO auto-generated method s

Tub String url = "Jdbc:mysql://127.0.0.1:3306/test";

String user = "root";

String password = "";

String Driver = "Com.mysql.jdbc.Driver";

Connection Conn =null;

PreparedStatement SQL =null;

ResultSet rs = null;

int id=0;

String Name=null;

float java = 0;

float ee = 0;

float aver = 0;

try{Class.forName (driver);

Conn =drivermanager.getconnection (Url,user,password);

SYSTEM.OUT.PRINTLN ("Connection database succeeded");

sql = conn.preparestatement ("select * FROM score where 1");

rs = Sql.executequery ();

while (Rs.next ()) {id = rs.getint (1);

Name = Rs.getstring (2);

java = rs.getfloat (3);

Java EE = rs.getfloat (4);

Aver = Rs.getfloat (5);

System.out.println ("ID:" +id+ "Name:" +name+ "Java:" +java+ "ee" +j2ee+ "Averagescroe:" +aver);

System.out.println ("Calculate the average score and deposit in the database");

sql = conn.preparestatement ("select * FROM score where 1");
rs = Sql.executequery ();
while (Rs.next ()) {id = rs.getint (1);

Name = Rs.getstring (2);

java = rs.getfloat (3);

Java EE = rs.getfloat (4);

Aver = (JAVA+J2EE)/2;

sql = conn.preparestatement ("Update score set averagescroe=" "+aver+" ' Where id= ' "+id+ '");

 

Sql.executeupdate ();

} System.out.println ("Output data");

sql = conn.preparestatement ("select * FROM score where 1");

rs = Sql.executequery ();

while (Rs.next ()) {id = rs.getint (1);

Name = Rs.getstring (2);

java = rs.getfloat (3);

Java EE = rs.getfloat (4);

Aver = Rs.getfloat (5);

System.out.println ("ID:" +id+ "Name:" +name+ "Java:" +java+ "ee" +j2ee+ "Averagescroe:" +aver);

} rs.close ();

Sql.close ();

 

 

 

Conn.close ();

 

}catch (Exception e) {e.printstacktrace ();}

  }

 

}


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.