java_Java快速入门练习.pdf
4 4.1 (1)(2)(3)(4)(5)JAVA 4.2 JAVA classobjectmethod 4 D-,;I-,;W-P-JAVA 4.3 Lab04,C:Lab04 Lab04 http:/javaLab/lab04.zip 4.4 .4.1:(D)4.4.1:(D)JAVA JAVA JAVA(class)(method)main applet paint JAVA JOptionPane showMessageDialog showInputDialog JOptionPane.showMessageDialog(null,Hi there!);String name=JoptionPane.showInputDialog(Whats your name?);JOptionPane import 1.1 import javax.swing.JOptionPane;import JAVA javax.swing JOptionPane Integer parseInt String int Interger import java.lang IntegerJava Java Java Java ObjectClass 2.4-1Student.java/Student.java public class Student private int id;/ID private String name;/public Student(int id,String name)this.id=id;this.name=name;public int getID()return id;public void setID(int id)this.id=id;public String getName()return name;public void setName(String name)this.name=name;Student 2 4 1 Student instance 3.JAVA JAVA Primitive Data TypeReference Data Type Student s1,s2;/s1=new Student/Student 0 xabcdef s1 s2=s1/s1 s2 2 4.1:4.1 s1 s2 s1 s2 method Java constructorinstance methodstatic method(1)constructor Student Student s=new Student(123,he xiang);new s new Student s(2)instance method.()objectReference.methodName(parameters)Student s getID s.getID();(3)static method JOptionPane showMessageDialog showInputDialog Integer parseInt.()ClassName.methodName(parameters)static methodstatic 4.3 (D)4.4.2:(D)Applet paint public void paint(Graphics g)()g Graphics()paint g Graphics paint Graphics drawString drawLine Font Font ft=new Font(Serif,Font.PLAIN,56);g.setFont(ft);ft Font ft ft setFont Graphics g Java Graphics setFont public abstract void setFont(Font font)font setFont Font ft ftmain String()public static void main(String args)4.4.3(D)4.4.3:(D)1.Java local variableinstance variableclass variableparameter static Java JVM Java pseudocode class someClass visibility_modifier variable_type instanceVariableName;/Instance Variable visibility_modifier static variable_type classVariableName;/Class Variable returnType someMethod1()variable_type localVariableName;/Local Variable returnType someMethod2(Type parameterName)/Parameter instanceVariableName classVariableName someClass instanceVariableName classVariableName localVariableName someMethod parameterName someMehod2“”4 instanceVariableName,classVariableName,localVariableName parameterName(1)(2)“$”“_”(3)“$”“_”(4)myName MyName xvalue-1$amount 2brotherroom#class()2.Java VBJAVASCRIPT:type identifier=value,identifier=value;int id;String name;3.4.Java 4.2 Java 4.2 5 catch if()int i=17;System.out.println(The value of i=+i);/if i if println if Java JVM ifwhile for if if Order orderDate Order orderDate orderDate JVM new JVM Order todayDate Order todayDate 5.6.VariableScope.java Java 4-2VariableScope.java/VariableScope.java public class VariableScope/classVariableName static public String classVariableName=;/instanceVariableName private int instanceVariableName=0;/intParameter oneMethod public void oneMethod(int intParameter)/temp oneMethod 6 int temp=5;if(intParameter temp)/JAVA /int temp=1;System.out.println(temp=+temp);instanceVariableName=instanceVariableName+intParameter;public void anotherMethod()System.out.println(+classVariableName);System.out.println(instanceVariableName=+instanceVariableName);public static void main(String args)/VariableScope /JVM VariableScope classVariableName VariableScope object1=new VariableScope();/VariableScope System.out.println(+VariableScope.classVariableName);System.out.println(+object1.classVariableName);/object1 instancevariable1,instancevariable2 object1.oneMethod(6);object1.anotherMethod();/object1 instanceVariableName 4.4.4:(I)(static)4.4.4:(I)1.Hello.java public class Hello public static void main(String args)System.out.println(Hello,world!);static main Java Hello static static-static “”“”static C 7 2.DrawX.java(4-3)ViewDrawX.html DrawX.java GraphicsUtility.java4-4 4-3DrawX.java/DrawX.java/Draws 3 big Xs./GraphicsUtility X.import java.applet.Applet;import java.awt.Graphics;import java.awt.Color;public class DrawX extends Applet public void paint(Graphics pen)setBackground(Color.black);pen.setColor(Color.white);GraphicsUtility.drawX(pen,0,0);GraphicsUtility.drawX(pen,4,0);GraphicsUtility.drawX(pen,29,35);/method paint(Graphics)/class DrawX.java “X”DrawX.java GraphicsUtility.java DrawX.java drawLine GraphicsUtility drawX GraphicsUtility drawX X public static void drawX(Graphics g,int x,int y)g.drawLine(x,y,x+200,y+130);g.drawLine(x,y+130,x+200,y);/method drawX(Graphics,int,int)drawX public static void drawX(Graphics g,int x,int y)()4-4GraphicsUtility.java import java.awt.Graphics;public class GraphicsUtility public static void drawX(Graphics g,int x,int y)g.drawLine(x,y,x+200,y+130);g.drawLine(x,y+130,x+200,y);/method drawX /class GraphicsUtility 8 call DrawX paint GraphicsUtility drawX GraphicsUtility.drawX(pen,0,0);GraphicsUtility.drawX(pen,4,0);GraphicsUtility.drawX(pen,29,35);“X”drawX paint drawX Java Graphics g=pen;int x=0;int y=0;(formal parameter)actual parameterlocal variableGraphicsUtility application applet main “extends Applet”DrawX.java drawX“X”GraphicsUtility drawZ drawX “Z”“X”GraphicUtility.java drawZ drawX drawZ public static void drawZ(Graphics g,int x,int y)x y“Z”“Z”20 30 DrawXZ.java GraphicsUtility drawZ drawX Z X 201 110 applet HTML ViewDrawXZ.htmlz Z 0018009050X 3.4-5ViewDrawXZ.html applet X Z?drawZ drawZ GraphicsUtility 9 4.4.5(D)4.4.5:(D)1.MathUtility.java 4-6MathUtility.java/MathUtility.java public class MathUtility public static double power(float base,int exponent)if(exponent 0)exponent=0-exponent;base=1/base;/if exponent 0 double product=1;for(int i=0;i exponent;i+)product*=base;return product;/method power(float,int)public static long power(int base,int exponent)if(exponent 0)System.err.println(:MathUtility.power(int,int)+exponent+0.);System.err.println(+);return 0;/if exponent 0 long product=1;for(int i=0;i exponent;i+)product*=base;return product;/method power(int,int)/class MathUtility power power float intpower int Java 2 2“method overload”10 MathUtility.java power power double power long PowerTest1.java 4-7 2.4-7PowerTest1.java/PowerTest1.java/Tests the two power methods/in class MathUtility public class PowerTest1 public static void main(String args)final int intBase=2;final int exponent=50;final float floatBase=intBase;long longPower=MathUtility.power(intBase,exponent);double doublePower=MathUtility.power(floatBase,exponent);System.out.println();/blank line System.out.println(intBase+exponent+:);System.out.println();System.out.println(+longPower+(long);System.out.println(+doublePower+(double unformatted);/method main /class PowerTest1 MathUtility power 2 50 double double 1.125899906842624E15 1.125899906842624 10 15 float double 2 10 PowerTest1.java MathUtility power long longPower=MathUtility.power(intBase,exponent);double doublePower=MathUtility.power(floatBase,exponent);power longPower doublePower y=f(x)z=g(x,y)Java“”“”Pascal“”“”C C+“11”MathUtility power return product;return power product 3.for(int i=0;i exponent;i+)product*=base;MathUtility power PowerTest2.java 4-8PowerTest2.java/PowerTest2.java/Tests the two power methods/in class MathUtility public class PowerTest2 public static void main(String args)final int intBase=2;final int exponent=-3;final float floatBase=intBase;long longPower=MathUtility.power(intBase,exponent);double doublePower=MathUtility.power(floatBase,exponent);System.out.println();/blank line System.out.println(intBase+exponent+:);System.out.println();System.out.println(+longPower+(long);System.out.println(+doublePower+(double);System.out.println();/method main /class PowerTest2 x 1/x power exponent power exponet 0 System.err System.err System.out System.out System.err DOS 12 4.PowerTest3.java main testPower 4-9PowerTest3.java/PowerTest3.java/Tests the two power methods/in class MathUtility public class PowerTest3 public static void main(String args)System.out.println();/blank line/testPower(2,50);/testPower.PowerTest3.testPower(2,61);/.PowerTest3.testPower(2,64);/method main public static void testPower(int intBase,int exponent)long longPower=MathUtility.power(intBase,exponent);float floatBase=intBase;double doublePower=MathUtility.power(floatBase,exponent);System.out.println(intBase+exponent+:);System.out.println();System.out.println(+longPower+(long);System.out.println(+doublePower+(double unformatted,scientific notation);System.out.println();/testPower /class PowerTest3 static ClassName.methodName(parameters)methodName(parameters)PowerTest3.java testPower 13 4.4.6:(D)class 4.4.6:(D)wrapper class Java int,char Boolean Byte Short CharacterIntegerLongFloatDouble DigitASCII.java 4-10DigitASCII.java/DigitASCII.java/ASCII Applet.import java.applet.Applet;import java.awt.Graphics;public class DigitASCII extends Applet public void paint(Graphics g)g.drawString(digit,40,40);g.drawString(character,27,55);g.drawString(ASCII,95,40);g.drawString(value,95,55);for(char digitChar=0;digitChar 15)System.out.println(Character);System.out.println(15 .);System.exit(0);/if user error System.out.println();System.out.println(System.out.println(System.out.println();String text=args0;18););for(int i=0;i text.length();i+)/:String positionText=Integer.toString(i);for(int j=positionText.length();j 2;j+)positionText=+positionText;System.out.print(+positionText);/:char x=text.charAt(i);System.out.print(+x+);/:if(Character.isDigit(x)System.out.println();else if(Character.isUpperCase(x)System.out.println();else if(Character.isLowerCase(x)System.out.println();else System.out.println();/for i /method main /class ClassifyChar Character if(Character.isDigit(x)System.out.println();else if(Character.isUpperCase(x)System.out.println();else if(Character.isLowerCase(x)System.out.println();else System.out.println();x char isDigit if API Character isDigit public static boolean isDigit(char ch)isDigit booleanCharacter“Method Summary”isLetterisLowerCase isUpperCase boolean ClassifyChar.java Character 19 4.4.10(D)10:4.4.10:(D)boolean boolean true falseTextUtility.java 4-15 boolean boolean 4-15TextUtility.java/TextUtility.java/*Contains some static methods useful*for dealing with characters and Strings.*/public class TextUtility /*Tests whether a character is in the ASCII range.*param x the character to be tested*return true if the characters Unicode value is in the ASCII range(0 to 127),*false otherwise*/public static boolean isAscii(char x)return(x=0&x=A&x=a&x=z);/metnod isAsciiLetter/*Tests whether a character is a space.*param x the character to be tested*return true if x is a space(),*false otherwise*/public static boolean isSpace(char x)return(x=);/method isSpace/*Tests whether a character is an*ASCII control character.*param x the character to be tested*return true if x is an ASCII control*character(0 to 31,or 127),*false otherwise*/public static boolean isAsciiControl(char x)return(x 32|x=127);/method isAsciiControl/*Tests whether a String contains*ASCII control characters.*param s the String to be tested 21 *return true if s contains one or more*ASCII control characters,*false otherwise.*/public static boolean containsAsciiControl(String s)boolean controlFound=false;for(int i=0;i s.length()&!controlFound;i+)if(isAsciiControl(s.charAt(i)controlFound=true;return controlFound;/method containsAsciiControl/*Rightjustify String*param s the String to be modify.*param columnWidth an int express the column width*return a new string of column width.*/public static String rightJustify(String s,int columnWidth)while(s.length()columnWidth)s=+s;return s;/method rightJustify/*A method to print a column of numbers aligned*at the decimal point.*param numberText a string t be convert*param wholeNumberWidth the total number*of characters to the left of the period*return a new string aligned at the decimal point*/public static String align(String numberText,int wholeNumberWidth)String leftPart=;/the left of decimal point String rightPart=;/the right of decimal point int index=numberText.indexOf(.);if(index=-1)leftPart=numberText;else 22 leftPart=numberText.substring(0,index);rightPart=numberText.substring(index);/else leftPart=TextUtility.rightJustify(leftPart,wholeNumberWidth);String align=leftPart+rightPart;return align;/method align(String,int)/class TextUtility 1.Java boolean/Unicode ASCII 0 127 if(x 128)boolean isAscii ASCII public static boolean isAscii(char x)return(x=0&x=0&x=A&x=a&x=z);/metnod isAsciiLetter isAsciiLetter isLetter Unicode ASCII 0 127 Character isLetter Unicode isAsciiControl public static boolean isAsciiControl(char x)return(x 32|x=127);/method isAsciiControl 2.23 3.TextUtility containsAsciiControl String ASCII public static boolean containsAsciiControl(String s)boolean controlFound=false;for(int i=0;i s.length()&!controlFound;i+)if(isAsciiControl(s.charAt(i)controlFound=true;return controlFound;/method containsAsciiControl /class TextUtility boolean controlFound false s ASCII boolean true 4.TextUtilityTest.java 4-16 testTextUtility else if(!TextUtility.isAscii(s.charAt(i)nonAsciiCount+;“”boolean main testTextUtility ASCII/Unicode 7 non-ASCII 4-16TextUtilityTest.java/TextUtilityTest.java public class TextUtilityTest public static void main(String args)testTextUtility(Hello,everyone.);testTextUtility(*bbbbbbbbb 1234);testTextUtility(Beep!u0007);testTextUtility(u00A0 u00A1 u00A2);/method main public static void testTextUtility(String s)System.out.print(nIn the String:+s+nthere are:);int asciiDigitCount=0;int asciiLetterCount=0;int spaceCount=0;int asciiControlCount=0;int nonAsciiCount=0;int asciiPunctuationCount=0;for(int i=0;i s.length();i+)if(TextUtility.isAsciiDigit(s.charAt(i)24 asciiDigitCount+;else if(TextUtility.isAsciiLetter(s.charAt(i)asciiLetterCount+;else if(TextUtility.isSpace(s.charAt(i)spaceCount+;else if(TextUtility.isAsciiControl(s.charAt(i)asciiControlCount+;else if(!TextUtility.isAscii(s.charAt(i)nonAsciiCount+;else/the only remaining alternative/is ASCII punctuation marks asciiPunctuationCount+;/for i System.out.println(asciiDigitCount+ASCII digits,+asciiLetterCount+ASCII letters,+spaceCount+spaces,n+asciiPunctuationCount+ASCII punctuation marks,+asciiControlCount+ASCII control characters,andn+nonAsciiCount+non-ASCII characters.);if(TextUtility.containsAsciiControl(s)System.out.println(It does contain control characters.);else System.out.println(It does not contain control characters.);/method testTextUtility /class TextUtilityTest 4.4.11(W)11:4.4.11:(W)boolean IntegerUtility 4-17 4-17IntegerUtility.java/*IntegerUtility.java*Contains some static methods useful for dealing with integers.*/public class IntegerUtility 25 /*Converts a digit character to its intended numeric value.*param x the character to be converted*return the intended numeric value of the character x,if x is a digit.*Returns-1 if x is not a digit.*/public static int toDigitValue(char x)if(TextUtility.isAsciiDigit(x)return(x-0);else return-1;/method toDigitValue/*An incomplete attempt to imitate the parseInt method of the Integer wrapper class.*Converts a String to an integer,if the String consist