java语言基础类

时间:2021-01-11 15:44:28

1.

public class Dog { String name;
int weight;
public Dog(String name, int weight) {
this.name = name;
this.weight = weight; }
public String toString()
{
return this.name + "重" + this.weight + "公斤";
}

public boolean equals(Object obj) {
if(this == obj) {
return true;
}
if(obj instanceof Dog) {
Dog dog = (Dog)obj;
if(dog.weight==this.weight&&dog.name.equals(this.name)) {
return true; }
}
return false; }
public int hashCode() {
int h;
int a=name.hashCode();
h = weight+a;
        return h;
}public static void main(String[] args) {    Dog dog1 = new Dog("DaHuan", 10);    Dog dog2 = new Dog("DaHuan", 10);    System.out.println(dog1);    System.out.println(dog1.equals(dog2));    System.out.println(dog1 == dog2); //地址} }


public class TestMath {
public static void main(String[] args) {
double a = Math.random() * 10; //double->会有损失
System.out.println("a=" + a);
System.out.println(a + "的平方根是:" + Math.sqrt(a));
}
}


2.

public class TestString {
public static void main(String[] args) {
String name = "My name is chen wei fang";
System.out.println("字符串的长度是:" + name.length());
System.out.println("字符串中的第一个字符是:" + name.charAt(0));
System.out.println("字符串中的最后一个字符是:" + name.charAt(name.length()-1));
System.out.println("字符串中的第一个单词是:" + name.substring(0,name.indexOf("y")+1));
System.out.println("字符串中的第一个单词是:" + name.substring(20,name.lastIndexOf("g")+1));
String s1 = new String("you are a student");
String s2 = new String("how are you");
if (s1.equals(s2)) {System.out.println("s1 与 s2 相同"); } else
{
System.out.println("s1 与 s2 不相同"); }
if(s1.startsWith("you")){
System.out.println("s1 是以 you 开头的"); }
if (s2.contains("you")) {
System.out.println("s2 中包含 you");
}
String path = "c:/java/A.java";
int index = path.indexOf("/");
System.out.println("c:/java/A.java 中第一个/出现的位置是:" + index);
int lastIndex = path.lastIndexOf("/");
System.out.println("c:/java/A.java 中最后一个/出现的位置是:" + lastIndex);
String fileName = path.substring(lastIndex+1,path.length());
System.out.println("c:ava/A.java 中包含的文件名是:" + fileName);
}
}


public class TestStringBuffer {
public static void main(String[] args) {
String s = "MicroSoft";
StringBuffer sb = new StringBuffer(s);
System.out.println(sb.append("Oracle"));
System.out.println(sb.insert(8,"/"));
System.out.println(sb.replace(0,8,"MicroSun"));
System.out.println(sb.delete(0,5));
} }

3.

import java.util.Random; 
import java.util.Scanner;
public class TestRandom {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in) ;
Random random = new Random();
int n = 0;
System.out.println("输入 0 表示退出循环");
while(true) {
System.out.print("请输入 n 的值:");
n = scanner.nextInt();
if(n==0) {
break; }
System.out.print("生成的[0," + n + "]之间的随机数是:");
System.out.println(random.nextInt(n+1));
}
System.out.println("程序执行结束");
scanner.close();
}}


4.

import sun.util.resources.CalendarData;

import java.util.Calendar;

public class TestCalendar {
public static void main(String[] args) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DATE,1);
int dayOfWeek =calendar.get(Calendar.DAY_OF_WEEK)-1;
int maxDate = calendar.getMaximum(Calendar.DATE);
System.out.println("日\t 一\t 二\t 三\t 四\t 五\t 六");
for(int i=Calendar.SUNDAY; i<dayOfWeek; i++) {
System.out.print("\t");
}
for(int i=1;i<=maxDate;i++) {
System.out.print(i + "\t");
if( (dayOfWeek+i-1)%7==0 ) {
System.out.println(); }
} }
}

5.

public class TestException1 {
static void m() throws Exception{
int a = 3;
int b = 0;
int c = a/b;
System.out.println(c);
}
public static void main(String[] args) {
try {
m();
} catch (Exception e) {
System.out.println("Wrong");
} finally
{
System.out.println("总会执行");
}
System.out.println("程序正常结束");
}
}

6.

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class TestException2 {
public static void main(String[] args)throws IOException{
BufferedReader br = new BufferedReader( new InputStreamReader(System.in));
try{
int x = Integer.parseInt(br.readLine());
int y = Integer.parseInt(br.readLine());
System.out.println(x/y);
br.close();
}
catch(Exception e){
e.printStackTrace();;
}
finally {
br.close();
}
} }

异常机制还是不太懂 需要看书和多多实践