java_Java快速入门练习.pdf

上传人:索**** 文档编号:76242993 上传时间:2023-03-08 格式:PDF 页数:29 大小:40.32KB
返回 下载 相关 举报
java_Java快速入门练习.pdf_第1页
第1页 / 共29页
java_Java快速入门练习.pdf_第2页
第2页 / 共29页
点击查看更多>>
资源描述

《java_Java快速入门练习.pdf》由会员分享,可在线阅读,更多相关《java_Java快速入门练习.pdf(29页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。

1、 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=Jo

2、ptionPane.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 pr

3、ivate 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 Dat

4、a 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.ge

5、tID();(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.PLA

6、IN,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 cl

7、ass 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 instanceVari

8、ableName 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:typ

9、e 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-2

10、VariableScope.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 /

11、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 clas

12、sVariableName 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)

13、(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

14、 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 pai

15、nt(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(Gra

16、phics 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.dr

17、awX(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”GraphicUt

18、ility.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.

19、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 exponen

20、t)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 MathUtil

21、ity.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=MathUtil

22、ity.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 M

23、athUtility 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“”“

24、”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

25、=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.

26、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 MathUtilit

27、y 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);floa

28、t 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 sta

29、tic 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.Gra

30、phics;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

31、 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);

32、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 i

33、f(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.jav

34、a 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

35、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

36、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 isAsciiContr

37、ol(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 controlFoun

38、d=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 stati

39、c 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*re

40、turn 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.subs

41、tring(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 isA

42、scii(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

43、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 te

44、stTextUtility 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 123

45、4);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 asciiPunctuationCou

46、nt=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(

47、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 c

48、haracters,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 IntegerUtili

49、ty 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,i

50、f 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

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 技术资料 > 实施方案

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号© 2020-2023 www.taowenge.com 淘文阁