Initial commit

This commit is contained in:
2025-11-06 10:29:13 +08:00
commit 0becd14830
318 changed files with 7145 additions and 0 deletions

View File

@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/5"/>
<classpathentry kind="lib" path="/ch10/WebContent/WEB-INF/lib/mysql-connector-java-5.1.44-bin.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>

8
Example_src/Chapter1-10/.idea/.gitignore generated vendored Normal file
View File

@@ -0,0 +1,8 @@
# 默认忽略的文件
/shelf/
/workspace.xml
# 基于编辑器的 HTTP 客户端请求
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="PROJECT" charset="UTF-8" />
</component>
</project>

5
Example_src/Chapter1-10/.idea/misc.xml generated Normal file
View File

@@ -0,0 +1,5 @@
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_21" default="true" project-jdk-name="21" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

View File

@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/javaEE.iml" filepath="$PROJECT_DIR$/javaEE.iml" />
</modules>
</component>
</project>

4
Example_src/Chapter1-10/.idea/vcs.xml generated Normal file
View File

@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings" defaultProject="true" />
</project>

View File

@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>javaEE</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
<filteredResources>
<filter>
<id>1741074534549</id>
<name></name>
<type>30</type>
<matcher>
<id>org.eclipse.core.resources.regexFilterMatcher</id>
<arguments>node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__</arguments>
</matcher>
</filter>
</filteredResources>
</projectDescription>

View File

@@ -0,0 +1,11 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.8

View File

@@ -0,0 +1,11 @@
DROP TABLE IF EXISTS `userinfo`;
CREATE TABLE `userinfo`
(
`userId` int(11) NOT NULL auto_increment,
`loginname` varchar(20) default NULL,
`password` varchar(10) default NULL,
PRIMARY KEY (`userId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `userinfo` (`userId`, `loginname`, `password`)
VALUES (1, 'admin', 'admin');

View File

@@ -0,0 +1,52 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="EclipseModuleManager">
<libelement value="file:///ch10/WebContent/WEB-INF/lib/mysql-connector-java-5.1.44-bin.jar" />
<src_description expected_position="1">
<src_folder value="file://$MODULE_DIR$/src" expected_position="1" />
</src_description>
</component>
<component name="NewModuleRootManager">
<output url="file://$MODULE_DIR$/bin" />
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="jdk" jdkName="JavaSE-1.8" jdkType="JavaSDK" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module-library">
<library name="junit5">
<CLASSES>
<root url="$MAVEN_REPOSITORY$/junit/junit/3.8.2/junit-3.8.2.jar" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
<orderEntry type="module-library">
<library name="mysql-connector-java-5.1.44-bin.jar">
<CLASSES>
<root url="file:///ch10/WebContent/WEB-INF/lib/mysql-connector-java-5.1.44-bin.jar" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
<orderEntry type="module-library">
<library name="JUnit5.8.1">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter/5.8.1/junit-jupiter-5.8.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-api/5.8.1/junit-jupiter-api-5.8.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/opentest4j/opentest4j/1.2.0/opentest4j-1.2.0.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/platform/junit-platform-commons/1.8.1/junit-platform-commons-1.8.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/apiguardian/apiguardian-api/1.1.2/apiguardian-api-1.1.2.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-params/5.8.1/junit-jupiter-params-5.8.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-engine/5.8.1/junit-jupiter-engine-5.8.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/platform/junit-platform-engine/1.8.1/junit-platform-engine-1.8.1.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
</component>
</module>

View File

@@ -0,0 +1,10 @@
package ch01;
import java.applet.Applet;
import java.awt.Graphics;
public class HelloApplet extends Applet {
public void paint(Graphics g) {
g.drawString("这是一个最简单的Applet程序", 40, 50);
}
}

View File

@@ -0,0 +1,14 @@
package ch01;
public class HelloWorld {
public static void main(String args[]) {
System.out.println("Hello World!");
int a = 123, b;
b = a >>> 33;
System.out.println("b=" + b);
int[] AB = new int[]{6, 80, 2, 55, 451};
System.out.println("AB[0]=" + AB[0]);
String[][] str1 = new String[][]{new String[3], new String[]{"hello"}};
System.out.println("str1[0][0]=" + str1[0]);
}
}

View File

@@ -0,0 +1,27 @@
package ch02;
public class ArrayDemo {
public static void main(String args[]) {
char[] AB;
AB = new char[3];
AB[0] = 'a';
AB[1] = 'b';
AB[2] = 'c';
System.out.println(AB[0]);
System.out.println(AB[1]);
System.out.println(AB[2]);
int[] AC = new int[2];
AC[0] = 100;
AC[1] = 200;
System.out.println(AC[0]);
System.out.println(AC[1]);
int[] AD = {10, 20, 30, 40};
System.out.println(AD[0]);
System.out.println(AD[1]);
System.out.println(AD[2]);
System.out.println(AD[3]);
System.out.println(AB.length);
System.out.println(AC.length);
System.out.println(AD.length);
}
}

View File

@@ -0,0 +1,9 @@
package ch02;
public class EscapeRout {
public static void main(String[] args) {
System.out.println("a\u0022.length() + \u0022b".length());
System.out.println("a".length() + "b".length());
System.out.println("a\".length()+\"b".length());
}
}

View File

@@ -0,0 +1,29 @@
package ch02;
public class LeapYear {
int year, month, day;
void setdate(int y, int m, int d) //成员方法,设置日期值
{ //公有的,无返回值,有三个参数
year = y;
month = m;
day = d;
}
boolean isleapyear() //判断年份是否为闰年
{ //布尔型返回值,无参数
return (year % 400 == 0) || (year % 100 != 0) && (year % 4 == 0);
}
void print() //输出日期值,无返回值,无参数
{
System.out.println("date is " + year + '-' + month + '-' + day);
}
public static void main(String args[]) {
LeapYear a = new LeapYear(); //创建对象
a.setdate(2012, 12, 25); //调用类方法
a.print();
System.out.println(a.year + " is a leap year? " + a.isleapyear());
}
}

View File

@@ -0,0 +1,45 @@
package ch02;
import java.util.Arrays;
public class Num2Rmb {
private String[] hanArr = {"", "", "", "", "", "", "", "", "", ""};
private String[] unitArr = {"", "", ""};
/*private String[] divide(double num)把一个浮点数分解成整数部分和小数部分字符串
num 需要被分解的浮点数,return 分解出来的整数部分和小数部分。
第一个数组元素是整数部分,第二个数组元素是小数部分 */
private String[] divide(double num) {
long zheng = (long) num; //将一个浮点数强制类型转换为long即得到它的整数部分
long xiao = Math.round((num - zheng) * 100);//浮点数减去整数部分乘以100再取整得到两位小数
//下面用了两种方法把整数转换为字符串
return new String[]{zheng + "", String.valueOf(xiao)};
}
/*private String toHanStr(String numStr)把一个四位的数字字符串变成汉字字符串
numStr 需要被转换的四位的数字字符串,return 四位的数字字符串被转换成的汉字字符串 */
private String toHanStr(String numStr) {
String result = "";
int numLen = numStr.length();
for (int i = 0; i < numLen; i++)//依次遍历数字字符串的每一位数字
{
int num = numStr.charAt(i) - 48;//把char型数字转换成的int型数字因为它们的ASCII码值恰好相差48
if (i != numLen - 1 && num != 0) //如果不是最后一位数字且数字不是零,则需要添加单位(仟、佰、拾)
{
result += hanArr[num] + unitArr[numLen - 2 - i];
} else //否则不要添加单位
{
result += hanArr[num];
}
}
return result;
}
public static void main(String[] args) {
Num2Rmb nr = new Num2Rmb();
System.out.println(Arrays.toString(nr.divide(326811125.45)));//测试把一个浮点数分解成整数部分和小数部分
System.out.println(nr.toHanStr("8906"));//测试把一个四位的数字字符串变成汉字字符串
}
}

View File

@@ -0,0 +1,20 @@
package ch02;
public class SymmetryArray {
public static void main(String[] args) {
int[] n = {1, 2, 0, 2, 1};
boolean flag = true; //假设对称
for (int i = 0; i < n.length / 2; i++) { //循环数组长度的一半次
if (n[i] != n[n.length - i - 1]) //比较元素
{
flag = false; //不对称
break; //结束循环
}
}
if (flag) {
System.out.println("对称");
} else {
System.out.println("不对称");
}
}
}

View File

@@ -0,0 +1,18 @@
package ch02;
import java.util.Arrays;
public class TestArrays {
public static void main(String[] args) {
int[] a = new int[]{3, 4, 5, 6}; // 定义一个a数组
int[] a2 = new int[]{3, 4, 5, 6}; // 定义一个a2数组
System.out.println("a数组和a2数组是否相等" + Arrays.equals(a, a2)); // 将输出true
int[] b = Arrays.copyOf(a, 6); // 通过复制a数组生成一个新的b数组
System.out.println("a数组和b数组是否相等" + Arrays.equals(a, b));
System.out.println("b数组的元素为" + Arrays.toString(b)); // 输出b数组的元素
Arrays.fill(b, 2, 4, 1); // 将b数组的第2个元素包含到第4个元素不包含赋值为1
System.out.println("b数组的元素为" + Arrays.toString(b)); // 输出b数组的元素
Arrays.sort(b); // 对b数组进行排序
System.out.println("b数组的元素为" + Arrays.toString(b)); // 输出b数组的元素
}
}

View File

@@ -0,0 +1,13 @@
package ch02;
public class TestLoan {
public static void main(String args[]) {
double dbp = 300000;
double dbi = 0.0459 / 12;
int nt = 30 * 12;
double dbpow = 1;
for (int i = 0; i < nt; i++) dbpow = dbpow * (1 + dbi);
double dbr = dbp * dbi * dbpow / (dbpow - 1);
System.out.println("你每月需还贷款R=" + dbr + "元/月");
}
}

View File

@@ -0,0 +1,18 @@
package ch02;
public class TestTwoDimension {
public static void main(String[] args) {
int[][] a; //定义一个二维数组
a = new int[3][]; //初始化a是一个长度为3的数组a数组的数组元素又是引用类型
for (int i = 0; i < a.length; i++) //把a数组当成一维数组遍历a数组的每个数组元素
{
System.out.println(a[i]);
}
a[0] = new int[2]; //初始化a数组的第一个元素
a[0][1] = 6; //访问a数组的第一个元素所指数组的第二个元素
for (int i = 0; i < a[0].length; i++) //a数组的第一个元素是一个一维数组遍历这个一维数组
{
System.out.println(a[0][i]);
}
}
}

View File

@@ -0,0 +1,12 @@
package ch02;
public class TestparseInt {
public static void main(String args[]) {
String str1 = "100";
String str2 = "12345678";
int int1 = Integer.parseInt(str1, 16);
long lng = Long.valueOf(str2).intValue();
System.out.println(int1);
System.out.println(lng);
}
}

View File

@@ -0,0 +1,12 @@
package ch02;
public class Testvalue {
public static void main(String args[]) {
long lng = 12345678;
int int1 = 100;
String str1 = String.valueOf(lng);
String str2 = Integer.toString(int1, 16).toUpperCase();
System.out.println(str1);
System.out.println(str2);
}
}

View File

@@ -0,0 +1,15 @@
package ch02;
public class TwoDementionArray {
public static void main(String args[]) {
float a[][];
a = new float[3][4];
a[0][0] = 100.0f;
a[0][1] = 100.1f;
a[2][1] = 102.1f;
System.out.println(a[0][0]);
System.out.println(a[0][1]);
System.out.println(a[2][1]);
System.out.println(a[2][3]);
}
}

View File

@@ -0,0 +1,29 @@
package ch02;
public class UseArray {
public static void main(String[] args) {
int[] s1 = {5, 6, 8, 21};
int[] s2 = new int[s1.length]; //s2必须先初始化分配空间
int[] s3;
int[] s4 = new int[s1.length]; //s4必须先初始化分配空间
prnt(s1);
System.out.println("s2 use for cycled method");
for (int i = 0; i < s1.length; i++) { //使用for循环复制数组
s2[i] = s1[i];
}
prnt(s2);
System.out.println("s3 use clone method");
s3 = (int[]) s1.clone();//使用clone创建sorces副本注意clone要使用强制转换
prnt(s3);
System.out.println("s4 use arraycopy method");
System.arraycopy(s1, 0, s4, 0, s1.length);
prnt(s4);
}
public static void prnt(int[] a) {
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
System.out.println();
}
}

View File

@@ -0,0 +1,19 @@
package ch02;
public class Variable {
public static void main(String[] args) {
char c = 'A';
int i = 1;
double d = 2.5;
boolean b = true;
final double SALES_TAX_RATE = 7.5;
String s = "Hello Again";
System.out.println("c =" + c);
System.out.println("i =" + i);
System.out.println("c =" + d);
System.out.println("b =" + b);
System.out.println("SALES TAX RATE =" + SALES_TAX_RATE);
System.out.println("s =" + s);
System.out.println("length of s =" + s.length());
}
}

View File

@@ -0,0 +1,12 @@
package ch03;
public class Average {
public static void main(String[] args) {
double n, sum = 0.0;
for (int i = 0; i < args.length; i++) {
sum = sum + Double.valueOf(args[i]).doubleValue();
}
n = sum / args.length;
System.out.println("平均数:" + n);
}
}

View File

@@ -0,0 +1,14 @@
package ch03;
public class BreakLoop {
public static void main(String args[]) {
int i, j;
for (i = 0; i < 6; i++) {
for (j = 0; j < 100; j++) {
if (j == 5) break;
System.out.print("*");
}
System.out.println();
}
}
}

View File

@@ -0,0 +1,18 @@
package ch03;
public class BuyChicken {
public static void main(String[] args) {
int I, J, K;
System.out.println(" 公鸡数I 母鸡数J 小鸡数K ");
for (I = 0; I <= 20; I++) // I 为公鸡数
{
for (J = 0; J <= 33; J++) // J 为母鸡数
{
K = 100 - I - J; // K 为小鸡数
if (5 * I + 3 * J + K / 3.0 == 100)
System.out.println(" " + I + " " + J + " " + K);
}
}
}
}

View File

@@ -0,0 +1,21 @@
package ch03;
import java.util.*;
public class ChangeLetter {
public static void main(String args[]) {
String s = new String("abcABC123");
System.out.println(s);
char a[] = s.toCharArray();
for (int i = 0; i < a.length; i++) {
if (Character.isLowerCase(a[i])) {
a[i] = Character.toUpperCase(a[i]);
} else if (Character.isUpperCase(a[i])) {
a[i] = Character.toLowerCase(a[i]);
}
}
s = new String(a);
System.out.println(s);
}
}

View File

@@ -0,0 +1,25 @@
package ch03;
class Con_Break2 {
public static void main(String args[]) {
int sum = 0, i, j;
for (i = 1; i <= 10; i++) //计算1+3+5+7+9.
{
if (i % 2 == 0)
continue;
sum = sum + i;
}
System.out.println("sum=" + sum);
for (j = 2; j <= 10; j++) //求10以内的素数
{
for (i = 2; i <= j / 2; i++) {
if (j % i == 0)
break;
}
if (i > j / 2) {
System.out.println("" + j + "是素数");
}
}
}
}

View File

@@ -0,0 +1,12 @@
package ch03;
import java.util.*;
public class ForDemo {
public static void main(String args[]) {
int a[] = {22, 2, 222, -1, -110, 0};
Arrays.sort(a);
for (int i = 0; i <= 5; i++) System.out.println(a[i] + ",");
}
}

View File

@@ -0,0 +1,10 @@
package ch03;
public class ForEachTest {
public static void main(String args[]) {
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8};
for (int a : arr)
System.out.println(a);
}
}

View File

@@ -0,0 +1,15 @@
package ch03;
public class IfDemo {
public static void main(String args[]) {
int c1 = 2;
int c2 = 3;
if (c1 == 1) {
System.out.println("c1=1");
} else if (c2 <= 1) {
System.out.println("c2<=1");
} else {
System.out.println("c1 != 1, c2>1");
}
}
}

View File

@@ -0,0 +1,13 @@
package ch03;
public class NestFor {
public static void main(String[] args) {
int i, j;
for (i = 0; i < 6; i++) {
for (j = 0; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
}
}

View File

@@ -0,0 +1,19 @@
package ch03;
import java.util.Scanner;
public class ReadKey {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int score = 0;
int sum = 0;
int count = -1;
System.out.println("输入分数(-1结束):");
while (score != -1) {
count++;
sum += score;
score = scanner.nextInt();
}
System.out.println("平均:" + (double) sum / count);
}
}

View File

@@ -0,0 +1,21 @@
package ch03;
public class SwitchDemo {
public static void main(String args[]) {
int c = 2;
switch (c) {
case 1:
System.out.println("case 1 c=" + c);
break;
case 3:
System.out.println("case 3 c=" + c);
break;
case 5:
System.out.println("case 5 c=" + c);
break;
default:
System.out.println("default c=" + c);
}
}
}

View File

@@ -0,0 +1,13 @@
package ch03;
public class TestLoan {
public static void main(String args[]) {
double dbp = 300000;
double dbi = 0.0459 / 12;
int nt = 30 * 12;
double dbpow = 1;
for (int i = 0; i < nt; i++) dbpow = dbpow * (1 + dbi);
double dbr = dbp * dbi * dbpow / (dbpow - 1);
System.out.println("你每月需还贷款R=" + dbr + "元/月");
}
}

View File

@@ -0,0 +1,14 @@
package ch03;
public class WhileSum {
public static void main(String args[]) {
double sum = 0, a = 1;
int i = 1;
while (i <= 20) {
a = a * (1.0 / i);
sum = sum + a;
i = i + 1;
}
System.out.println("sum=" + sum);
}
}

View File

@@ -0,0 +1,42 @@
package ch04;
abstract class Animal {
public abstract void eat(); //只有方法名,而没有方法体
}
class Dog extends Animal {
private String name;
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void eat() {
System.out.println("小狗啃骨头啦");
}
}
class Bird extends Animal {
public void fly() {
System.out.println("小鸟飞呀飞呀");
}
public void eat() {
System.out.println("小鸟吃虫子啦");
}
}
public class BackAnimal {
public static void main(String args[]) {
Animal an = new Dog();
an.eat();
an = new Bird();
an.eat();
}
}

View File

@@ -0,0 +1,31 @@
package ch04;
class 类人猿 {
public 类人猿() {
System.out.println("类人猿 Created!");
}
void crySpeak() {
System.out.println(" 我是类人猿。");
}
}
class People1 extends 类人猿 {
public People1() {
System.out.println("People Created!");
}
void crySpeak() {
System.out.println(" 我是People。");
}
}
class BackwardObject {
public static void main(String[] args) {
类人猿 originalPeople = new 类人猿();
People1 people = new People1();
originalPeople = people;
originalPeople.crySpeak();
people.crySpeak();
}
}

View File

@@ -0,0 +1,41 @@
package ch04;
class Sum {
int n;
float f() {
float sum = 0;
for (int i = 1; i <= n; i++)
sum = sum + i;
return sum;
}
}
class Average extends Sum {
int n;
float f() {
float c;
super.n = n;
c = super.f();
return c / n;
}
float g() {
float c;
c = super.f();
return c / 2;
}
}
public class CallHidenVar {
public static void main(String[] args) {
Average aver = new Average();
aver.n = 100;
float result_1 = aver.f();
float result_2 = aver.g();
System.out.println("result_1=" + result_1);
System.out.println("result_2=" + result_2);
}
}

View File

@@ -0,0 +1,19 @@
package ch04;
class Fact {
float fact(int n) { //定义计算n!的方法
int i;
float x = 1;
for (i = 1; i < n; i++)
x = x * n;
return x;
}
}
public class CheckFact {
public static void main(String[] args) {
Fact x = new Fact();
System.out.println(x.fact(10)); //计算10
System.out.println(x.fact(15)); //计算15
}
}

View File

@@ -0,0 +1,33 @@
package ch04;
import java.applet.*;
import java.awt.*;
class Area {
float fun(float r) {
return 3.14159f * r * r;
}
float get(float x, float y) {
return x + y;
}
}
class Circle extends Area {
float fun(float r) {
return 3.14159f * 2.0f * r;
}
}
public class CircleArea extends Applet {
Circle yuan;
public void init() {
yuan = new Circle();
}
public void paint(Graphics g) {
g.drawString("调用子类重写的方法fun(): 园的面积= " + yuan.fun(5.0f), 5, 20);
g.drawString("调用继承父类的方法get(): x+y=" + yuan.get(12.0f, 8.0f), 5, 40);
}
}

View File

@@ -0,0 +1,18 @@
package ch04;
import java.io.*;
public class Crctngle {
public static void main(String[] args) {
Rectangle x1 = new Rectangle(); //声明类的对象并实例化
Rectangle x2 = new Rectangle(50, 40); //声明类的对象并初始化
Rectangle x3 = new Rectangle(x1);
System.out.println("x1.length()=" + x1.length());
System.out.println("x1.width()=" + x1.width());
System.out.println("x2.length()=" + x2.length());
System.out.println("x2.width()=" + x2.width());
System.out.println("x3.length()=" + x3.length());
System.out.println("x3.width()=" + x3.width());
}
}

View File

@@ -0,0 +1,19 @@
package ch04;
class ABC //friendly类
{
public int pub_i = 5;
public void show() {
System.out.println("pub_i=" + pub_i);
}
}
class Demo {
public static void main(String[] args) {
ABC abc = new ABC(); //使用本包中的friendly类
System.out.println("abc.pub_i=" + abc.pub_i);
abc.pub_i = 10;
abc.show();
}
}

View File

@@ -0,0 +1,20 @@
package ch04;
class A {
void callme() {
System.out.println("Inside A's callme( ) method");
}
}
class B extends A {
void callme() {
System.out.println("Inside B's callme( ) method");
}
}
public class Dispatch {
public static void main(String args[]) {
A a = new B();
a.callme();
}
}

View File

@@ -0,0 +1,31 @@
package ch04;
class 动物 {
void cry() {
}
}
class extends 动物 {
void cry() {
System.out.println("狗cry():汪汪.....");
}
}
class extends 动物 {
void cry() {
System.out.println("猫cry(): 喵喵.....");
}
}
class DynamicState {
public static void main(String[] args) {
动物 dongwu;
if (Math.random() >= 0.5) {
dongwu = new (); //上转的对象
dongwu.cry();
} else {
dongwu = new (); //上转的对象
dongwu.cry();
}
}
}

View File

@@ -0,0 +1,11 @@
package ch04;
class Father {
void speak() { //无参数的speak()方法
System.out.println("I am Father! ");
}
void speak(String s) { //有参数的speak(String s)方法
System.out.println("I like" + " " + s + ". ");
}
}

View File

@@ -0,0 +1,37 @@
package ch04;
class superClass {
int x;
superClass() {
x = 3;
System.out.println("in superClass : x = " + x);
}
void doSomething() {
System.out.println("in superClass.doSomething( )");
}
}
class subClass extends superClass {
int x;
subClass() {
super(); //call constructor of superClass
x = 5;
System.out.println("in subClass : x = " + x);
}
void doSomething() {
super.doSomething(); //call method of superClass
System.out.println("in subClass.doSomething( )");
System.out.println("super.x = " + super.x + " sub.x = " + x);
}
}
public class Inheritance {
public static void main(String[] args) {
subClass subC = new subClass();
subC.doSomething();
}
}

View File

@@ -0,0 +1,21 @@
package ch04;
class Fibi {
public long fibinacii(int n) {
long c = 0;
if (n == 1 || n == 2)
c = 1;
else
c = fibinacii(n - 1) + fibinacii(n - 2);
return c;
}
}
public class ItemsFibi {
public static void main(String[] args) {
Fibi a = new Fibi();
for (int i = 1; i <= 10; i++) {
System.out.print(" " + a.fibinacii(i));
}
}
}

View File

@@ -0,0 +1,24 @@
package ch04;
import java.io.*;
class MethodOverloading {
void receive(int i) {
System.out.println("Receive one int data");
System.out.println("i=" + i);
}
void receive(int x, int y) {
System.out.println("Receive two int datas");
System.out.println("x=" + x + " y=" + y);
}
}
public class MethodOverloadingTest {
public static void main(String[] args) {
MethodOverloading mo = new MethodOverloading();
mo.receive(1);
mo.receive(2, 3);
}
}

View File

@@ -0,0 +1,9 @@
package ch04;
public class OverLoadingDemo {
public static void main(String[] args) {
Father x = new Father();
x.speak(); //调用无参的speak()方法
x.speak("music"); //调用有参的speak("music")方法
}
}

View File

@@ -0,0 +1,38 @@
package ch04;
import ch04.tom.langshan.NewDate;
public class People {
private String name;
private NewDate birth;
public static void main(String[] args) {
People a = new People("XiaoYu", 1989, 10, 06);
a.output();
}
public People(String n1, NewDate d1) {
name = n1;
birth = d1;
}
public People(String n1, int y, int m, int d) {
this(n1, new NewDate(y, m, d));
}
public People() {
this("", new NewDate());
}
public int age() { //计算年龄
return NewDate.thisyear() - birth.year(); //获得年份
}
public void output() {
System.out.println("name : " + name);
System.out.println("birth: " + birth.toString());
System.out.println("age : " + age());
}
}

View File

@@ -0,0 +1,31 @@
package ch04;
import java.util.Scanner;
public class PrimeNumber {
/*用(int) Math.sqrt(n)求出循环上限
* isPrime()方法用来检测当前数是否为质数
*/
public static boolean isPrime(int num) {
boolean prime = true;
int limit = (int) Math.sqrt(num);
for (int i = 2; i <= limit; i++) {
if (num % i == 0) {
prime = false;
break;
}
}
return prime;
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("请输入您要判断的数:");
int n = input.nextInt();
if (isPrime(n)) {
System.out.println(n + "是质数!");
} else {
System.out.println(n + "不是质数!");
}
}
}

View File

@@ -0,0 +1,20 @@
package ch04;
public class PrvtVar {
private int money;
PrvtVar() {
money = 2000;
}
private int getMoney() {
return money;
}
public static void main(String[] args) {
PrvtVar exa = new PrvtVar();
exa.money = 3000;
int m = exa.getMoney(); //可访问本类成员的private变量
System.out.println("money=" + m);
}
}

View File

@@ -0,0 +1,29 @@
package ch04;
class Rectangle { //矩形类
private int width; //矩形的宽度
private int length; //矩形的长度
Rectangle() { //矩形的不带参数的构造函数缺省的给出长30和宽20
length = 30;
width = 20;
}
Rectangle(int l, int w) { //矩形的带参数的构造函数
length = l;
width = w;
}
Rectangle(Rectangle r) { //此构造方法以另一个Rectangle作为参数
width = r.width(); //通过对象调用函数并附值给相应变量
length = r.length();
}
int width() { //返回宽度
return width;
}
int length() { //返回长度
return length;
}
}

View File

@@ -0,0 +1,18 @@
package ch04;
import java.applet.Applet;
import java.awt.*;
public class RedButton extends Applet {
Button redbutton;
public void init() {
redbutton = new Button("我是一个红色的按钮");
redbutton.setBackground(Color.red);
add(redbutton);
}
public void paint(Graphics g) {
g.drawString("it is a button", 30, 50);
}
}

View File

@@ -0,0 +1,40 @@
package ch04;
class Student {
int number, tel = 81238888;
String name;
Student(int number, String name) {
this.number = number;
this.name = name;
System.out.println("Father构造:I am " + name + "my number is " + number);
}
void show() {
System.out.println("father's show(tel): tel of Student is " + tel);
}
}
class Univer_Student extends Student {
boolean 婚否;
int tel = 81236666;
Univer_Student(int number, String name, boolean b) {
super(number, name); // 使用父类的构造方法
婚否 = b;
System.out.println("Son构造新增属性:婚否=" + 婚否);
}
void showtel() {
System.out.println("Son's showtel(tel):tel of Univer_Student is " + tel);
System.out.println("Son's showtel(super.tel):tel of super is " + super.tel);
}
}
public class ShowStudent {
public static void main(String[] args) {
Univer_Student zhang = new Univer_Student(8030410, " XiaoBin", false);
zhang.showtel();
zhang.show();
}
}

View File

@@ -0,0 +1,38 @@
package ch04;
import java.applet.*;
import java.awt.*;
class Father1 {
private int money;
float weight, height;
String head;
String speak(String s) {
return s;
}
}
class Son extends Father1 {
String hand, foot;
}
public class SubBoy extends Applet {
Son boy;
public void init() {
boy = new Son();
boy.weight = 120f;
boy.height = 1.75f;
boy.head = "一个聪明的大脑袋,";
boy.hand = "两只巧手,";
boy.foot = "一双喜欢瞎跑的脚。";
}
public void paint(Graphics g) {
g.drawString(boy.speak("我是儿子"), 5, 20);
g.drawString(boy.head + boy.hand + boy.foot, 5, 40);
g.drawString("体重:" + boy.weight + " 身高:" + boy.height, 5, 60);
}
}

View File

@@ -0,0 +1,58 @@
package ch04;
class {
double 半径;
(double r) {
半径 = r;
}
double 计算面积() {
return 3.14 * 半径 * 半径;
}
void 修改半径(double 新半径) {
半径 = 新半径;
}
double 获取半径() {
return 半径;
}
}
class 圆锥 {
底圆;
double ;
圆锥( circle, double h) {
this.底圆 = circle;
this. = h;
}
double 计算体积() {
double volume;
volume = 底圆.计算面积() * / 3.0;
return volume;
}
void 修改底圆半径(double r) {
底圆.修改半径(r);
}
double 获取底圆半径() {
return 底圆.获取半径();
}
}
public class Taper {
public static void main(String args[]) {
circle = new (10);
圆锥 circular = new 圆锥(circle, 20);
System.out.println("圆锥底圆半径:" + circular.获取底圆半径());
System.out.println("圆锥的体积:" + circular.计算体积());
circular.修改底圆半径(100);
System.out.println("圆锥底圆半径:" + circular.获取底圆半径());
System.out.println("圆锥的体积:" + circular.计算体积());
}
}

View File

@@ -0,0 +1,20 @@
package ch04;
public class TempConverter {
public static void main(String[] args) {
TempConverter t = new TempConverter();
t.data();
}
protected void data() {
for (int i = -40; i <= 120; i += 10) {
float c = (i - 32) * (5f / 9);
print(i, c);
}
}
protected void print(float f, float c) {
System.out.println("华氏温度" + f + "=摄氏温度" + c);
}
}

View File

@@ -0,0 +1,29 @@
package ch04;
import java.applet.*;
import java.awt.*;
class Chengji {
float fun(float x, float y) {
return x * y;
}
}
class AddChengji extends Chengji {
float fun(float x, float y) {
return x + y;
}
}
public class TestAddChengji extends Applet {
AddChengji sum;
public void init() {
sum = new AddChengji();
}
public void paint(Graphics g) {
g.drawString("sum=" + sum.fun(4, 6), 100, 40);
}
}

View File

@@ -0,0 +1,23 @@
package ch04;
class Point {
int x, y;
Point() {
x = 0;
y = 0;
}
Point(int x, int y) {
this.x = x;
this.y = y;
}
}
public class TestPoint {
public static void main(String[] args) {
Point p1 = new Point(34, 56);
Point p2 = new Point(21, 67);
p1 = p2;
}
}

View File

@@ -0,0 +1,28 @@
package ch04;
class Text1 {
static int a; //当被定义为static类型时为类变量可被对象或类调用
int b; //对象变量,只能被对象调用
public void display(int a, int b) { //成员方法
System.out.println("static int a=" + a);
System.out.println(" int b=" + b);
}
public static void display(int b) {
System.out.println("static display: int b=" + b);
}
public static void main(String args[]) {
Text1 tt = new Text1();
tt.display(5, 6);
//Text1.display(5,6)是错误的,对象变量或对象方法只能被对象tt调用
Text1.display(0);
//当被定义为static类型时为类方法可被对象或类调用
tt.display(23);
tt.a = 9;
Text1.a = 24;
tt.b = 3;
tt.display(a, 15);
}
}

View File

@@ -0,0 +1,22 @@
package ch04;
import java.applet.*;
import java.awt.*;
class Tongji {
protected float fun(float x, float y) {
return x * y;
}
}
class Addxy extends Tongji {
// float fun(float x,float y) { //非法,因为降低了访问级别
// return x+y ;
// }
}
class Subxy extends Tongji {
public float fun(float x, float y) { //合法,没有降低访问级别.
return x - y;
}
}

View File

@@ -0,0 +1,19 @@
package ch04.sun.com;
import ch04.tom.langshan.Father;
public class Jerry extends Father { //Jerry和Father在不同的包中.
public Jerry() {
super(20);
}
public static void main(String[] args) {
Jerry jerry = new Jerry();
// jerry.height=12; //非法,因为Jerry没有继承友好的height.
jerry.weight = 200; //合法.
jerry.money = 800; //合法.
int m = jerry.getMoney(); //合法..
// jerry.setMoney(300); //非法,因为Jerry没有继承友好的方法setMoney.
System.out.println("m=" + m);
}
}

View File

@@ -0,0 +1,19 @@
package ch04.tom.langshan;
public class Father {
int height;
protected int money;
public int weight;
public Father(int m) {
money = m;
}
protected int getMoney() {
return money;
}
void setMoney(int newMoney) {
money = newMoney;
}
}

View File

@@ -0,0 +1,29 @@
package ch04.tom.langshan;
import java.util.*; //引用java.util包
public class NewDate {
private int year, month, day;
public NewDate(int y, int m, int d) {
year = y;
month = (((m >= 1) & (m <= 12)) ? m : 1);
day = (((d >= 1) & (d <= 31)) ? d : 1);
}
public NewDate() {
this(0, 0, 0);
}
public static int thisyear() { //获得当年的年份
return Calendar.getInstance().get(Calendar.YEAR);
}
public int year() { //获得年份
return year;
}
public String toString() { //转化为字符串
return year + "-" + month + "-" + day;
}
}

View File

@@ -0,0 +1,27 @@
package ch05;
interface People {
void peopleList();
}
class Student implements People {
public void peopleList() {
System.out.println("Im a student.");
}
}
class Teacher implements People {
public void peopleList() {
System.out.println("Im a teacher.");
}
}
public class BackInterface {
public static void main(String[] args) {
People a; // 声明接口变量
a = new Student(); // 实例化,接口变量中存放对象的引用
a.peopleList(); // 接口回调
a = new Teacher(); // 实例化,接口变量中存放对象的引用
a.peopleList(); // 接口回调
}
}

View File

@@ -0,0 +1,31 @@
package ch05;
class 梯形 {
float 上底, ;
static float 下底;
梯形(float x, float y, float h) {
上底 = x;
下底 = y;
= h;
}
float 获取下底() {
return 下底;
}
void 修改下底(float b) {
下底 = b;
}
}
public class CommonLader {
public static void main(String[] args) {
梯形 laderOne = new 梯形(3.0f, 10.0f, 20), laderTwo = new 梯形(2.0f, 3.0f, 10);
System.out.println("laderOne的下底:" + laderOne.获取下底());
System.out.println("laderTwo的下底:" + laderTwo.获取下底());
laderTwo.修改下底(60);
System.out.println("laderOne的下底:" + laderOne.获取下底());
System.out.println("laderTwo的下底:" + laderTwo.获取下底());
}
}

View File

@@ -0,0 +1,35 @@
package ch05;
interface 收费 {
public void 收取费用();
}
class 公共汽车 implements 收费 {
public void 收取费用() {
System.out.println("公共汽车:一元/张,不计算公里数");
}
}
class 出租车 implements 收费 {
public void 收取费用() {
System.out.println("出租车:1.60元/公里,起价3公里");
}
}
class 电影院 implements 收费 {
public void 收取费用() {
System.out.println("电影院:门票,十元/张");
}
}
class ExampleInterface {
public static void main(String[] args) {
公共汽车 七路 = new 公共汽车();
出租车 大众 = new 出租车();
电影院 更俗剧场 = new 电影院();
七路.收取费用();
大众.收取费用();
更俗剧场.收取费用();
}
}

View File

@@ -0,0 +1,38 @@
package ch05;
class Member {
static int classVar;
int instanceVar;
static void setClassVar(int i) {
classVar = i;
// instanceVar=i; // 类方法不能访问实例变量
}
static int getClassVar() {
return classVar;
}
void setInstanceVar(int i) {
classVar = i; //实例方法不但可以访问类变量,也可以实例变量
instanceVar = i;
}
int getInstanceVar() {
return instanceVar;
}
}
public class MemberTest {
public static void main(String[] args) {
Member m1 = new Member();
Member m2 = new Member();
m1.setClassVar(1);
m2.setClassVar(2);
System.out.println("m1.classVar=" + m1.getClassVar() + " m2.ClassVar=" + m2.getClassVar());
m1.setInstanceVar(11);
m2.setInstanceVar(22);
System.out.println("m1.InstanceVar=" + m1.getInstanceVar() + " m2.InstanceVar=" + m2.getInstanceVar());
}
}

View File

@@ -0,0 +1,21 @@
package ch05;
class Rectangle { //矩形类
int width; // 矩形的宽
int usethis(int width) { //返回宽度的函数
this.width = width; //指自己这个对象
return width;
}
int unusethis(int width) {
int w = width;
return w;
}
public static void main(String[] args) {
Rectangle r = new Rectangle(); //类对象的实例化
System.out.println("r.width=" + r.width + " r.usethis(1)=" + r.usethis(1) + " r.width=" + r.width);
System.out.println("r.width=" + r.width + " r.unusethis(2)=" + r.unusethis(2) + " r.width=" + r.width);
}
}

View File

@@ -0,0 +1,58 @@
package ch05;
abstract class 图形 {
public abstract double 求面积();
}
class 梯形1 extends 图形 {
double a, b, h;
梯形1(double a, double b, double h) {
this.a = a;
this.b = b;
this.h = h;
}
public double 求面积() {
return ((1 / 2.0) * (a + b) * h);
}
}
class 圆形 extends 图形 {
double r;
圆形(double r) {
this.r = r;
}
public double 求面积() {
return (3.14 * r * r);
}
}
class {
图形 ;
double ;
(图形 , double ) {
this. = ;
this. = ;
}
void 换底(图形 ) {
this. = ;
}
public double 求体积() {
return (.求面积() * ) / 3.0;
}
}
public class ShapeArea {
public static void main(String[] args) {
zui;
图形 tuxing;
tuxing = new 梯形1(2.0, 7.0, 10.7);
System.out.println("梯形的面积" + tuxing.求面积());
zui = new (tuxing, 30);
System.out.println("梯形底的堆的体积" + zui.求体积());
tuxing = new 圆形(10);
System.out.println("半径是10的圆的面积" + tuxing.求面积());
zui.换底(tuxing);
System.out.println("圆形底的堆的体积" + zui.求体积());
}
}

View File

@@ -0,0 +1,16 @@
package ch05;
import static java.lang.Math.sqrt; //导入静态方法sqrt()
import static java.lang.Math.pow; //导入静态方法pow()
class Hypot {
public static void main(String args[]) {
double side1, side2;
double hypot;
side1 = 3.0;
side2 = 4.0;
// 静态导入后不再需要通过类名Math来调用方法sqrt()和pow()
hypot = sqrt(pow(side1, 2) + pow(side2, 2));
System.out.println("给定RT△的两边边长为:" + side1 + "" + side2 + ",其斜边边长为:" + hypot);
}
}

View File

@@ -0,0 +1,27 @@
package ch05;
public class Stu1 implements Student_info //实现学生情况接口
{
String name;
int birth_year; //类自己的成员变量
public Stu1(String n1, int y) {
name = n1;
birth_year = y;
}
public int age() //实现接口的方法
{
return year - birth_year;
}
public void output() //实现接口的方法
{
System.out.println(this.name + " " + this.age() + "");
}
public static void main(String[] args) {
Stu1 s1 = new Stu1("李明", 1980);
s1.output();
}
}

View File

@@ -0,0 +1,9 @@
package ch05;
interface Student_info {
int year = 2002;
int age();
void output();
}

View File

@@ -0,0 +1,40 @@
package ch05;
import java.io.*;
class SuperClass { //定义父类
int x;
SuperClass() { //父类的构造方法
x = 10;
}
void doClass() {
System.out.println("SuperClass.doClass()");
}
}
class SubClass extends SuperClass { //定义子类
int x;
SubClass() { //子类的构造方法
super(); //调用父类的构造方法
x = 100;
}
void doClass() { //重写父类的doClass方法
System.out.println("SubClass.doClass()");
}
void doDemo() { //演示super和this的方法
int x;
x = 1000;
super.doClass(); //调用父类的doClass方法
doClass(); //调用本类的doClass方法
System.out.println("super.x=" + super.x); //父类的x
System.out.println("this.x=" + this.x); //本类的x
System.out.println("x=" + x); //本方法的x
}
}
public class SuperDemo {
public static void main(String[] args) { //主方法
SubClass s = new SubClass();
s.doDemo();
}
}

View File

@@ -0,0 +1,22 @@
package ch05;
abstract class C {
abstract void callme();
void metoo() {
System.out.println("Inside C's metoo( ) method");
}
}
class D extends C {
void callme() {
System.out.println("Inside D's callme( ) method");
}
}
public class TestAbstract {
public static void main(String[] args) {
C c = new D();
c.callme();
c.metoo();
}
}

View File

@@ -0,0 +1,53 @@
package ch05;
import java.applet.*;
import java.awt.*;
interface Computable {
final int MAX = 100;
void speak(String s);
int f(int x);
float g(float x, float y);
}
class China implements Computable {
int xuehao;
public int f(int x) { //不要忘记public关键字.
int sum = 0;
for (int i = 1; i <= x; i++) {
sum = sum + i;
}
return sum;
}
public float g(float x, float y) {
return 6; //至少有return语句.
}
public void speak(String s) {
}
}
class Japan implements Computable {
int xuehao;
public int f(int x) {
return 68;
}
public float g(float x, float y) {
return x + y;
}
public void speak(String s) {
//必须有方法体,但体内可以没有任何语句.
}
}
public class TestInterface {
public static void main(String[] args) {
China Li;
Japan Henlu;
Li = new China();
Henlu = new Japan();
Li.xuehao = 991898;
Henlu.xuehao = 941448;
System.out.println("学号:" + Li.MAX + Li.xuehao + "从1到100 求和" + Li.f(100));
System.out.println("学号:" + Henlu.MAX + Henlu.xuehao + "加法" + Henlu.g(2.0f, 3.0f));
}
}

View File

@@ -0,0 +1,22 @@
package ch05;
class 三角形 {
double a, b, c;
三角形(double a, double b, double c) {
setABC(this, a, b, c);
}
void setABC(三角形 trangle, double a, double b, double c) {
trangle.a = a;
trangle.b = b;
trangle.c = c;
}
}
class Triangle {
public static void main(String[] args) {
三角形 tra = new 三角形(3, 4, 5);
System.out.print("三角形型的三边是:" + tra.a + "," + tra.b + "," + tra.c + ",");
}
}

View File

@@ -0,0 +1,48 @@
package ch06;
import java.util.ArrayList;
import java.util.List;
class Student {
private String name;
private int score;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
public Student(String name, int score) {
super();
this.name = name;
this.score = score;
}
public String toString() {
return name + "的成绩为:" + score + "";
}
}
public class ArrGen {
public static void main(String[] args) {
Student stu1 = new Student("Smith", 70);
Student stu2 = new Student("John", 89);
List<Student> newsStu = new ArrayList<Student>();
newsStu.add(stu1);
newsStu.add(stu2);
for (Student s : newsStu) {
System.out.println(s);
}
}
}

View File

@@ -0,0 +1,19 @@
package ch06;
import java.util.*;
public class ArrayListExample {
public static void main(String[] args) {
List list = new ArrayList();
list.add("1");
list.add("2");
list.add(2, "3");
System.out.println("list集合有" + list.size() + "个元素");
list.remove(1);
System.out.println("删除一个元素后list集合有" + list.size() + "个元素");
if (list.contains("3")) {
System.out.println("集合中存在元素\"3\"");
}
System.out.println("\"1\"的索引位置是:" + list.indexOf("1"));
}
}

View File

@@ -0,0 +1,21 @@
package ch06;
import java.util.Calendar;
public class CalendarExample {
public static void main(String[] args) {
Calendar cal = Calendar.getInstance();
System.out.println("当前的日期和时间:");
System.out.print("年-月-日 ");
System.out.println("时:分:秒");
System.out.print(cal.get(Calendar.YEAR) + "-");
System.out.print(cal.get(Calendar.MONTH) + "-");
System.out.print(cal.get(Calendar.DATE) + " ");
System.out.print(cal.get(Calendar.HOUR) + ":");
System.out.print(cal.get(Calendar.MINUTE) + ":");
System.out.println(cal.get(Calendar.SECOND));
cal.add(Calendar.DATE, 5);
System.out.print("5天后是");
System.out.println(cal.get(Calendar.DATE) + "");
}
}

View File

@@ -0,0 +1,20 @@
package ch06;
//使用两种方法,比较两个字符串是否相等
public class CompareString {
public static void main(String[] args) {
String str1 = new String("We are happy!");
String str2 = new String("We are happy!");
System.out.println("使用==运算符比较时:");
if (str1 == str2)
System.out.println("两个字符串相等");
else
System.out.println("两个字符串不相等");
System.out.println();
System.out.println("使用equals()方法比较时:");
if (str1.equals(str2))
System.out.println("两个字符串相等");
else
System.out.println("两个字符串不相等");
}
}

View File

@@ -0,0 +1,15 @@
package ch06;
import java.util.Date;
public class DateExample {
public static void main(String[] args) {
Date dt = new Date();
System.out.println("今天的日期是:" + dt); //A
String strDate = dt.toString(); //B
String strMonth = strDate.substring(4, 7); //C
System.out.println("现在的月份是:" + strMonth);
String strTime = strDate.substring(11, 19); //D
System.out.println("当前的时间是:" + strTime);
}
}

View File

@@ -0,0 +1,16 @@
package ch06;
public class EnvExample {
public static void main(String[] args) {
double d1 = 3.2;
Double objd1 = new Double(d1);
Double objd2 = new Double("3.2");
System.out.println("最大的double值" + Double.MAX_VALUE);
System.out.println("最小的double值" + Double.MIN_VALUE);
System.out.println("将字符串转换成double值" + Double.parseDouble("23.2"));
System.out.println("Double对象中封装的值" + objd1.doubleValue());
System.out.println("比较objd1==objd2:" + (objd1 == objd2));
System.out.println("比较objd1.equals(objd2):" + objd1.equals(objd2));
}
}

View File

@@ -0,0 +1,20 @@
package ch06;
import java.util.HashMap;
import java.util.Map;
public class HashMapExample {
public static void main(String[] args) {
Map map = new HashMap();
map.put("one", "");
map.put("two", "");
map.put("three", "");
System.out.println(map.get("one")); //A
System.out.println(map.values()); //B
map.put("three", "3"); //C
System.out.println(map.values()); //D
System.out.println("删除前的键集" + map.keySet()); //E
map.remove("one"); //F
System.out.println("删除后的键集" + map.keySet()); //G
}
}

View File

@@ -0,0 +1,26 @@
package ch06;
import java.util.HashMap;
import java.util.Map;
public class HashMapExampleEdit {
public static void main(String[] args) {
Map<String, String> map = new HashMap<String, String>();
map.put("one", "");
map.put("two", "");
map.put("three", "");
System.out.println(map.get("one"));
System.out.println(map.values());
map.put("three", "3");
System.out.println(map.values());
System.out.println("删除前的键集" + map.keySet());
map.remove("one");
System.out.println("删除后的键集" + map.keySet());
for (Map.Entry<String, String> entry : map.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
System.out.println("key=" + key + ", value=" + value);
}
}
}

View File

@@ -0,0 +1,21 @@
package ch06;
import java.util.*;
public class HashSetExample {
public static void main(String[] args) {
Set set = new HashSet();
set.add("1");
set.add("2");
set.add("3");
set.add(new Integer(2));
set.add(new Double(3.2));
set.add("3");
System.out.println("set集合的元素个数为" + set.size() + "");
System.out.println("set集合的元素分别为" + set.toString());
}
}

View File

@@ -0,0 +1,16 @@
package ch06;
import java.text.SimpleDateFormat;
import java.util.Date;
public class SimpleDateExample {
public static void main(String[] args) throws Exception {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); //A
Date dt = sdf.parse("2009-8-30"); //B
System.out.println("英文格式:" + dt);
System.out.println();
sdf.applyPattern("yyyy年MM月dd日"); //C
String str = sdf.format(dt); //D
System.out.println("中文格式:" + str);
}
}

View File

@@ -0,0 +1,20 @@
package ch06;
import java.util.Scanner;
public class TotalMoney {
public static void main(String[] args) {
double totalMon, UnitPrice;
int num;
Scanner sc = new Scanner(System.in);
System.out.println("依次输入单价、数量,并以/分隔:");
String str = sc.nextLine();
Scanner scan = new Scanner(str);
scan.useDelimiter("/");
UnitPrice = scan.nextDouble();
num = scan.nextInt();
totalMon = UnitPrice * num;
System.out.println("总金额为:" + totalMon);
}
}

View File

@@ -0,0 +1,19 @@
package ch06;
public class extractAndsearch {
public static void main(String[] args) {
String str1 = new String("中华人民共和国60周年庆");
String str2 = new String(" 国庆大阅兵 ");
int loc1 = str1.indexOf('国');
int loc2 = str1.indexOf("人民");
int loc3 = str1.lastIndexOf('人');
int loc4 = str1.lastIndexOf("60");
System.out.println("loc1=" + loc1);
System.out.println("loc2=" + loc2);
System.out.println("loc3=" + loc3);
System.out.println("loc4=" + loc4);
System.out.println(str2.substring(2));
System.out.println(str2.substring(3, 6));
}
}

View File

@@ -0,0 +1,26 @@
package ch07;
import java.util.InputMismatchException;
import java.util.Scanner;
public class ComputeAvg {
public static void main(String[] args) {
int score, num;
double avg;
Scanner in = new Scanner(System.in);
try {
System.out.println("请输入总分:");
score = in.nextInt();
System.out.println("请输入人数:");
num = in.nextInt();
avg = score / num;
System.out.println("平均成绩为:" + avg);
} catch (InputMismatchException e1) {
System.out.println("输入不是数字!");
} catch (ArithmeticException e2) {
System.out.println("人数不能为0");
} catch (Exception e3) {
System.out.println("其他异常:");
}
}
}

View File

@@ -0,0 +1,40 @@
package ch07;
import java.util.Scanner;
class MyException extends Exception { //继承Exception类
private String myString;
public MyException(String myString) {
this.myString = myString;
}
public String getMyString() {
return myString;
}
@Override
public String toString() { //重写了类的方法
return myString;
}
}
public class ExceptionTest {
public static void main(String[] args) {
double i, j;
Scanner scan = new Scanner(System.in);
System.out.println("请输入分子:");
i = scan.nextDouble();
System.out.println("请输入分母");
j = scan.nextDouble();
try {
if (j == 0) { //判断分母的值
throw new MyException("除数不能零!"); //A
} else
System.out.println("分数值为:" + i + "/" + j + "=" + (i / j));
} catch (MyException e) {
System.out.println(e);
}
}
}

View File

@@ -0,0 +1,23 @@
package ch07;
import java.util.Scanner;
public class checkDemo {
public static void check() {
Scanner in = new Scanner(System.in);
System.out.println("请输入商品的书目:");
int num = in.nextInt();
if (num < 0)
throw new NumberFormatException("商品书目不能小于0");
else
System.out.println("商品数目为:" + num);
}
public static void main(String[] args) {
try {
check();
} catch (Exception e) {
System.out.println("异常:" + e.getMessage());
}
}
}

View File

@@ -0,0 +1,26 @@
package ch07;
import java.io.IOException;
import java.util.Scanner;
public class throwsDemo {
public static void test() throws NullPointerException, IOException {
System.out.println("请输入一个数字01");
Scanner in = new Scanner(System.in);
int flag = in.nextInt();
if (flag == 1)
throw new NullPointerException();
else
throw new IOException();
}
public static void main(String[] args) {
try {
test();
} catch (NullPointerException e) {
System.out.println("系统抛出了" + e.getClass() + "类型的异常");
} catch (IOException e) {
System.out.println("系统抛出了" + e.getClass() + "类型的异常");
}
}
}

View File

@@ -0,0 +1,26 @@
package ch08;
import java.io.File;
public class FileAndDirectoryTest {
public static void main(String[] args) {
String filename = "testFile";
File f1 = new File(filename); //A
System.out.println(filename + "是否存在:" + f1.exists());
System.out.println(filename + "是文件吗:" + f1.isFile());
System.out.println(filename + "最后修改时间:" + f1.lastModified());
System.out.println(filename + "文件大小:" + f1.length());
String direname = "testDire";
File f2 = new File(direname); //B
System.out.println(direname + "的绝对路径:" + f2.getAbsolutePath());
if (f2.isDirectory()) { //C
String[] fileList = f2.list(); //D
System.out.println(direname + "目录中的文件和目录包括:");
for (int i = 0; i < fileList.length; i++)
System.out.print(fileList[i] + " ");
}
}
}

View File

@@ -0,0 +1,25 @@
package ch08;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class FileInExample {
public static void main(String[] args) {
int rs;
byte b[] = new byte[10];
try {
File f = new File("C:\\in.txt");
FileInputStream fis = new FileInputStream(f);
rs = fis.read(b, 0, 10);
while (rs > 0) {
String s = new String(b, 0, rs);
System.out.print(s);
rs = fis.read(b, 0, 10);
}
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

View File

@@ -0,0 +1,28 @@
package ch08;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileOutExample {
public static void main(String[] args) {
int rs;
byte b[] = new byte[10];
try {
FileInputStream fis = new FileInputStream("notepad.exe");
FileOutputStream fos = new FileOutputStream("note.exe");
System.out.println("<EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ժ<EFBFBD>......");
rs = fis.read(b, 0, 10);
while (rs > 0) {
fos.write(b, 0, 10);
rs = fis.read(b, 0, 10);
}
System.out.println("<EFBFBD>ļ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>лл<EFBFBD><EFBFBD>");
fis.close();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

Some files were not shown because too many files have changed in this diff Show More