已阅读5页,还剩15页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
第一章1、JAVA语言规范Java语言规范定义了java的语法,java库则在API中定义。JDK是用于开发和运行java程序的软件。IDE是快速开发程序的集成开发环境。API:应用程序接口,也称为库,包括为开发java程序而预定义的类和接口。JDK:是由一套独立程序构成的集合,每个程序都是从命令行调用的,用于开发和测试java程序。IDE:集成开发环境。2、JAVA 的三个版本JAVA标准版(JAVA SE):可以用来开发客户端的应用程序。应用可以独立运行或作为applet在Web浏览器中运行。JAVA企业版(JAVA EE):可以用来开发服务器端的运用程序。例如:Java servlet 和Javasever Pages(JSP),以及Javasever Faces(JSF)。JAVA微型版(JAVA ME):用来开发移动设备的应用程序。例如:手机。3、创建、编译和执行JAVA程序Java源程序保存为.Java文件,编译为.class文件。.class文件由Java虚拟机(JVM)执行。第二章1、Java提供简单数据类型来表示整数、实数、字符以及布尔类型。这些类型称为原始数据类型或基本类型。2、命名常量final datatype CONSTANTNAME = value 例:final double PI = 3.14159;3、显示当前时间public class ShowCurrentTime public static void main(String args) long totalMilliseconds = System.currentTimeMillis();long totalSeconds = totalMilliseconds / 1000;long currentSecond = totalSeconds % 60;long totalMinutes = totalSeconds / 60;long currentMinute = totalMinutes % 60;long totalHours = totalMinutes / 60;long currentHour = totalHours % 24;System.out.println(Current time is + currentHour + : + currentMinute + : + currentSecond + GTM);结果:Current time is 17:31:8 GMT4、自增和自减操作符操作符名称说明示例(假设i=1)+var前置自增操作符变量var的值加一,且使用var增加后的新值int j = +ivar+后置自增操作符变量var的值加一,但使用var原来的值int j = i+-var前置自减操作符变量var的值减一,且使用var减少后的新值int j = -ivar-后置自减操作符变量var的值减一,但使用var原来的值int j = i- 第三章1、boolean数据类型声明一个具有值true或者false的变量2、Switch语句的运用例1:十二生肖import java.util.Scanner;public class ChineseZodiac public static void main(String args) Scanner input = new Scanner(System.in);System.out.print(Enter a year: );int year = input.nextInt();switch (year % 12) case 0: System.out.println(monkey);break;case 1: System.out.println(rooster);break;case 2: System.out.println(dog);break;case 3: System.out.println(pig);break;case 4: System.out.println(rat);break;case 5: System.out.println(ox);break;case 6: System.out.println(tiger);break;case 7: System.out.println(rabbit);break;case 8: System.out.println(dragon);break;case 9: System.out.println(snake);break;case 10: System.out.println(horse);break;case 11: System.out.println(sheep);例2:给出一个月的总天数import java.util.Scanner;public class nut public static void main(String args) Scanner input = new Scanner(System.in);System.out.print(Enter a year: );int year = input.nextInt();System.out.print(Enter a month: );String month = input.next();int flag = 0;String monthly = Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec;int day = 31,28,31,30,31,30,31,31,30,31,30,31;for(int i = 0;i = 0的数字,返回x的平方根3、Math类中的取整方法方法描述ceil(x)x向上取整为它最接近的整数。该整数作为一个双精度值返回floor(x)x向下取整为它最接近的整数。该整数作为一个双精度值返回rint(x)x取整为它最接近的整数。如果x与两个整数的距离相等,偶数的整数作为一个双精度值返回round(x)如果x是单精度数,返回(Int)Math.floor(x + 0.5);如果x是双精度数,返回(long)Math.floor(x + 0.5)4、字符的比较和测试Character类中的方法方法描述isDigit(ch)如果指定的字符是一个数字,返回trueisLetter(ch)如果指定的字符是一个字母,返回trueisLetterOrDigit(ch)如果指定的字符是一个字母或者数字,返回trueisLowerCase(ch)如果指定的字符是一个小写字母,返回trueisUpperCase(ch)如果指定的字符是一个大学字母,返回truetoLowerCase(ch)返回指定的字符的小写形式toUpperCase(ch)返回指定的字符的大写形式5、String类型String对象的简单方法方法描述length()返回字符串中的字符数charAt(index)返回字符串s中指定位置的字符concat(s1)将本字符串和字符串s1连接,返回一个新字符串toUpperCase()返回一个新字符串,其中所有的字母大写toLowerCase()返回一个新字符串,其中所有的字母小写trim()返回一个新字符串,去掉了两边的空白字符6、字符串比较String对象的比较方法方法描述equals(s1)如果该字符串等于字符串s1,返回truecomtareTo(s1)返回一个大于0、等于0、小于0的整数,表明一份字符串是否大于、等于或者小于s17、获得子字符串String类包含的获取子串的方法方法描述substring(beginIndex)返回该字符串的子串,从特定位置beginIdex的字符开始到字符串的结尾。8、获得字符串中的字符或者子串String类包含获取子串的方法方法描述indexOf(ch)返回字符串中出现的第一个ch的下标。如果没有匹配的,返回-1indexOf(ch,fromIndex)返回字符串中fromIndex之后出现的第一个ch的下标。如果没有匹配的,返回-1indexOf(s)返回字符串中出现的第一个字符串s的下标。如果没有匹配的,返回-1indexOf(s,fromIndex)返回字符串中fromIndex之后出现的第一个字符串s的下标。如果没有匹配的,返回-19、将十六进制数转换为十进制数import java.util.Scanner;public class HexDigit2Dec public static void main(String args) Scanner input = new Scanner(System.in);System.out.print(Enter a hex digit: );String hexString = input.nextLine();if (hexString.length() != 1)System.out.println(You must enter exactly one character);System.exit(1);char ch = Character.toUpperCase(hexString.charAt(0);if (ch = A)int value = ch - A + 10;System.out.println(The decimal value for hex digit + ch + is + value);else if (Character.isDigit(ch)System.out.println(The decimal value for hex digit + ch + is + ch);elseSystem.out.println(ch + is an invalid input);结果:Enter a hex digit:AB7C You must enter exactly one charaterEnter a hex digit:BThe decimal value for hex digit B is 11第五章1、将十进制数转换为十六进制数import java.util.Scanner;public class Dec2Hex public static void main(String args) Scanner input = new Scanner(System.in);System.out.print(Enter a decimal number : );int decimal = input.nextInt();String hex = ;while (decimal != 0) int hexValue = decimal % 16; char hexDigit = (hexValue = 0) ? (char)(hexValue + 0) : (char)(hexValue - 10 + A); hex = hexdigit + hex; decimal = decimal / 16;System.out.println(The hex number is + hex);结果:Enter a decimal number: 1234The hex number is 4D2第六章1、重载方法public static double max(double num1 , double num2) if (num1 num2) return num1; else return num2;重载maxpublic class TestMethodOverding public static void main(String args) System.out.println(The maximum of 3 and 4 is + max(3, 4);System.out.println(The maxumum of 3.0 and 5.4 is + max(3.0, 5.4);System.out.println(The maximum of 3.0, 5.4, and 10.14 is + max(3.0, 5.4, 10.14);public static int max(int num1, int num2)if(num1 num2)return num1;else return num2;public static double max (double num1, double num2)if(num1 num2)return num1;else return num2;public static double max(double num1, double num2, double num3)return max(max(num1, num2), num3);结果:The maximum of 3 and 4 is 4The maximum of 3.0 and 5.4 is 5.4The maximum of 3.0 ,5.4 , and 10.14 is 10.142、检测密码6.18import java.util.Scanner;public class sixAndEighteen public static boolean isPassword(String password) int flag = 0,count = 0;if(password.length() = 8) for(int i = 0;i = 0 & password.charAt(i) = a & password.charAt(i) = A& password.charAt(i) = 2)return true;return false;public static void main(String args) Scanner input = new Scanner(System.in);String password = input.next();if(isPassword(password)System.out.println(Valid Password);elseSystem.out.println(Invaild Password);财务应用程序:信用卡号的合法性6.31import java.util.Scanner;public class SixAndthreeOne public static boolean isVaild(long number) if(getDigit(sumOfDoubleEvenPlace(number)+sumOfOddPlace(number) = 1) & (getSize(number) = 13 | getSize(number) = 16)&(prefixMatched(number,4) | prefixMatched(number, 5)| prefixMatched(number, 37) | prefixMatched(number, 6)return true;return false;public static int sumOfDoubleEvenPlace(long number) long num = number;long sum = 0;while(number != 0) num = number % 100 / 10;number = number / 100;if(num * 2 = 10)num = (num*2 % 10) + (num*2 / 10);elsenum = num * 2;sum += num;return (int)sum;public static int getDigit(int number) return number % 10 = 0?1:0;public static int sumOfOddPlace(long number) long num = number % 10;number /= 10;while(number != 0) num += number % 100 / 10;number /= 100;return (int)num;public static boolean prefixMatched(long number,int d) if(getPrefix(number, getSize(number) - 2)= d | getPrefix(number, getSize(number) - 2) / 10 = d)return true;return false;public static int getSize(long d) int i = 0;while(d != 0)d /= 10;i+;return i;public static long getPrefix(long number ,int k) number = number / (long)Math.pow(10,k);return number;public static void main(String args) Scanner input = new Scanner(System.in);long number = input.nextLong();if(isVaild(number)System.out.println(number+is valid);elseSystem.out.println(number+is invalid);第七章1、创建数组arrayRefVar = new elementTypearraySize1) 使用new elementTypearraySize 创建了一个数组;2) 把这个新创建的数组的引用赋值给arrayRefVar。(元素类型 数组引用变量 = new 元素类型 数组大小 )(元素类型 数组引用变量 = new 元素类型 数组大小)例:double myList = new double 10;2、数组大小和默认值使用arrayRefVav.length得到数组的大小。例如:myList.length为10.创建数组后,他的元素被赋予默认值,数值型(int)基本数据类型的默认值为0,char型的默认值为u0000,boolean型的默认值为false。3、数组的查找二分查找法public class seventoseven public static int binarySearch(int list,int key)int low = 0;int high = list.length - 1;while (high = low)int mid = (low + high) / 2;if(key listmid)high = mid - 1;else if(key = listmid)return mid;else low = mid + 1;return -low - 1;5、Arrays类1)使用sort或者parallelSort方法对整个数组或部分数组进行排序double number = 6.0,4.0,1.9,2.9,3.4,3.5Java.util.Arrays.sort(number);java.util.Array.parallelSort(number);char chars = a,A,4,F,D,PJava.util.Arrays.sort(chars,1,3);java.util.Array.parallelSort(chars,1,3);2)使用二分查找法(binarySearch 方法)在数组中查找关键字。数组必须按升序排列好,如果数组中不存在关键字,方法返回 -(插入点下标 + 1)。例如int list = 2,4,7,10,11,45,50,59,60,66,69,70,79;System.out.println(“1.Index is ” + java.util.Arrays.binarySearch(list,11);System.out.println(“2.Index is ” + java.util.Arrays.binarySearch(list,12);char chars = a,c,g,x,y,z;System.out.println(“3.Index is ” + java.util.Arrays.binarySearch(chars,a);System.out.println(“4.Index is ” + java.util.Arrays.binarySearch(chars,t);结果:1. Index is 42. Index is -63. Index is 04. Index is -43)使用equals方法检测两个数组是否相等。(如果相等返回ture ,否则false)System.out.println(java.util.Arrays.equals(list1,list2);System.out.println(java.util.Arrays.equals(list2,list3);4)使用fill方法填充整个数组或者部分数组。例如:下列代码将5填充到list1中,将8填充到list2 1-list5-2中int list1 = 2,4,7,10int list2 = 2,4,7,7,7,10java.util.Arrays.fill(list1,5);java.util.Arrays.fill(list2,1,5,8)5)使用toString方法来返回一个字符串,该字符串代表了数组中的所有元素。这是一个显示数组中所有元素的快捷和简便的方法。例如:int list = 2,4,7,10;System.out.println(Arrays.toString(list);显示 2, 4, 7, 10 6)使用Arrays.copy()第八章1、定位最大的元素8.13import java.util.Scanner;public class eightAndthreeteen public static int locateLargest(double a) int location = new int2;double max = 0;for(int i = 0;i a.length;i+) for(int j = 0;j ai.length;j+) if(max aij)max = aij;location0 = i;location1 = j;return location;public static void main(String args) Scanner input = new Scanner(System.in);int n,m;n = input.nextInt();m = input.nextInt();double a = new doublenm;for(int i = 0;i n;i+)for(int j = 0;j m;j+)aij = input.nextDouble();System.out.println(The location of the largest element is at (+locateLargest(a)0+,+locateLargest(a)1+);第九章1、为对象定义类,类为对象定义属性和行为,对象(object)代表现实世界中可以明确标识的一个实体。每个对象都有自己独特的标识、状态(特征、属性)和行为(动作)。数据域构造方法方法class Circle double radius = 1;Circle() Circle(double newRadius) radius = newRadius;double getArea() return radius * radius * Math.PI;double getPrimeter() return 2 * raduis * Math.PI;void setRadius(double newRadius) radius = newRadius;类名数据域构造方法和方法UML类图Circleradius:doubleCircle( )Circle(nweRadius: double)getArea( ): doublegetPrimeter( ): doublesetRadius(newRadius: double): void对象的UML符号circle1:Circlecircle2:Circlecircle3:Circleradius = 1radius = 25radius = 125使用UML符号表示类和对象2、定义类和创建对象TV符号+表示公共修饰符public这个TV的当前频道(从1到120)这个TV的当前音量(从1到7)表明这个TV是开的还是关的构造一个默认的TV对象打开这个TV关闭这个TV为这个TV设置一个新频道为这个TV设置一个新音量给频道数增加1给频道数减去1给音量增加1给音量减去1channel: intvolumLevel: inton: boolean+TV( )+turnOn( ): void+turnoff( ): void+setChannel(newChannel: int): void+setVolume(newVolumeLevel: int): void+channelUp( ):void+channelDown( ): void+volumeUp( ): void+volumeDown( ): void3、引用数据域和NULL值引用类型数据域的默认值第NULL,数值类型数据域的默认值是0,boolean类型数据域的默认值是false,而char类型数据域的默认值是u0000。但是java没有给方法中的局部变量赋默认值。4、静态变量、常量和方法9-6 圆的声明public class CircleWithStaticMembers double radius;static int numberOfObjects;public CircleWithStaticMembers() radius = 1;numberOfObjects+;public CircleWithStaticMembers(double newRadius) radius = newRadius;numberOfObjects+;static int getNumberOfObjects() return numberOfObjects;double getArea() return radius * radius * Math.PI;9-7 如何使用实例变量、静态变量、实例方法和静态方法,以及使用效果public static void main(String args) System.out.println(Before creating objects);System.out.println(The number of Circle objects is + CircleWithStaticMembers.numberOfObjects);CircleWithStaticMembers c1 = new CircleWithStaticMembers();System.out.println(nAfter creating c1);System.out.println(c1: radius (+c1.radius + ) and number of Circle objects( + c1.numberOfObjects+);CircleWithStaticMembers c2 = new CircleWithStaticMembers();c1.radius = 9;System.out.println(nAfter creating c2 and modifying c1);System.out.println(c1: radius (+c1.radius + ) and number of Circle objects( + c1.numberOfObjects+);System.out.println(c2: radius (+c2.radius + ) and number of Circle objects( + c2.numberOfObjects+);结果:Before creating objectsThe number of Circle objects is 0After creating c1c1: radius(1.0) and number of Circle objects(1)After creating c2 and modifying c1c1: radius(9.0) and number of Circle objects(2)c2: radius(5.0) and number of Circle objects(2)5、可见性修饰符可见性修饰符可以用于确定一个类以及它的成员的可见性(package)私有的修饰符将访问权限限定在它自己的类内,默认修饰符将访问权限限定在包内,而公共的修饰符可以无限制的访问package p1;public class C1 public int x; int y; private int z; public void m1( ) void m2( )private void m3( ) package p1;public class C2 void aMethod( ) C1 o = new C1( );can access o.x;can access o.y;cannot access o.z;can invoke o.m1( );can invoke o.m2( );cannot invoke o.m3( ); package p2;public class C3 void aMrthod( ) C1 o = new C1( );can access o.x;cannot access o.y;cannot access o.z;can invoke o.m1( );cannot invoke o.m2( );cannot invoke o.m3( ); 如果一个类没有被定义为公共类,那么它只能在同一个包内被访问。如下所示,C2可以访问C1,而C3不能访问C1。一个非公共类具有包访问性package p1;class C1 package p1;public class C2 can access C1package p2;public class C3 cannot access C1;can access C2;6、this引用关键字this引用对象自身。它也可以在构造方法内部用于调用同一个类的其他构造方法。关键字this是指向调用对象本身的引用名。可以用this关键字引用对象的实例成员。在引用隐藏数据域以及调用一个重载的构造方法的时候,this引用是必须的。1) 使用this引用隐藏数据域private int I = 5;public void setI(int i) this.i = I;2) 使用this调用构造方法 关键字this可以用于调用同一类的另一个构造方法。 public class Circle private double radius;this关键字用于引用所构建的对象的隐藏数据域radiusthis关键字用于调用另一个构造方法 public Circle (double radius) this.radius = radius; public Circle ( ) this(1.0);this(1.0)调用带double值参数的第一个构造方法。java要求在构造方法中,语句this(参数列表)应用在任何其他可执行语句之前出现。7、栈类10-7 栈类储存数字,逆序显示public static void main(String args)StackOfIntegers stack = new StackOfIntegers();for(int i =0 ;i = elements.length) int temp = new intelements.length * 2;System.arraycopy(elements, 0, temp, 0,elements.length);elements = temp;elementssize+ = value;public int pop() return elements-size;public int peek() return elementssize - 1;public boolean empty() return size = 0;publi
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 柳州遴选笔试真题及答案
- 物业公司业主投诉处理和回访制度(5篇)
- 2025年版《痛风及高尿酸血症基层诊疗指南》解读(全文)
- 2025年孝感市孝南区保安员招聘考试题库附答案解析
- 2025年全科医学综合知识考查试卷答案及解析
- 2012湖南事业单位招考公共基础知识模拟试题
- 公务员国考历年行测真题试卷及答案
- 2025年湖南省永州市江华县保安员招聘考试题库附答案解析
- 2025年中医气血津液辨证中医方剂学冲刺试题卷
- 2023年高校教师资格证之高等教育学通关题库(附答案)
- 统编版(2024)八年级上册道德与法治第九课 积极奉献社会 复习课件
- 婚姻法课件教学课件
- 储装运安全培训课件
- GB/T 45816-2025道路车辆汽车空调系统用制冷剂系统安全要求
- 2025《煤矿安全规程》新旧对照专题培训
- 电厂保洁服务合同范本
- 湖北成人学士学位英语真题及答案
- 音乐课程标准核心解读
- 一级代理授权书模板
- 互联网农业课件
- 血透感染控制监测
评论
0/150
提交评论