練習一:類的設計
先新建一個Person
public class Person {
String name;
int age;
/**
* sex:1 表明是男性
* sex:0 表明是女性
*/
int sex;
public void study(){
System.out.println("studying");
}
public void showAge(){
System.out.println("age:" + age);
}
public int addAge(int i){
age += i;
return age;
}
}
再建一個PersonTest來調用Person方法
public class PersonTest {
public static void main(String[] args) {
Person p1 = new Person();
p1.name = "Tom";
p1.age = 18;
p1.sex = 1;
p1.study();
p1.showAge();
int newAge = p1.addAge(2);
System.out.println(p1.name + "的新年齡爲:" + newAge);
System.out.println(p1.age);//輸出的是20
//*************************
Person p2 = new Person();
p2.showAge();//輸出的是0,因爲新建了一個
p2.addAge(10);//賦值
p2.showAge();//輸出的是10,
p1.showAge();
}
}
練習二:類的設計
利用面向編程方法,設計類Circle計算圓的面積
方式一
public class Test {
public static void main(String[] args) {
Circle c1 = new Circle();//創建一個對像
c1.radius = 2.1;//給半徑一個數,隨便賦值
double area = c1.findArea();
System.out.println(area);
}
}
// 圓
class Circle {
// 屬性,定義一個半徑
double radius;
// 求圓的面積
public double findArea() {
double area = Math.PI * radius * radius;//這裏直接調用了
//double area = 3.14 * radius * radius;和上面的一樣,只是沒有那麼精細
return area;
}
}
方式二
public class Test {
public static void main(String[] args) {
Circle c1 = new Circle();
c1.radius = 2.1;// 給半徑賦值
c1.findArea();
}
}
//圓
class Circle {
// 屬性,定義了一個半徑
double radius;
// 求圓的面積
public void findArea() {
double area = Math.PI * radius * radius;// 這裏直接調用了
// double area = 3.14 * radius * radius;和上面的代碼一樣,只是沒有那麼精細
System.out.println("面積爲:" + area);
}
}
錯誤的示例
public class CircleTest {
public static void main(String[] args) {
Circle c1 = new Circle();
c1.radius = 2.1;
double area = c1.findArea(3.4);
System.out.println(area);
}
}
//圓
class Circle{
public double findArea(double r){//不能直接在裏面定義形參
double area = 3.14 * r * r;
return area;
}
}
練習三:方法聲明
- 編寫程序,聲明一個method方法,在方法中打印一個108的型矩形,在main方法中調用該方法。
- 修改上一個程序,在method方法中,除打印一個108的型矩形外,再計算該矩形的面積,並將其作爲方法返回值。在main方法中調用該方法,接收返回的面積值並打印。
- 修改上一個程序,在method方法提供m和n兩個參數,方法中打印一個mn的型矩形,並計算該矩形的面積,將其作爲方法返回值。在main方法中調用該方法,接收返回的面積值並打印。
第一問:
public class Test {
public static void main(String[] args) {
Test test = new Test();
test.method();
}
public void method() {
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 8; j++) {
System.out.print("* ");
}
System.out.println();
}
}
}
第二問:
public class Test{
public static void main(String[] args) {
Test test = new Test();
int area = test.method();
System.out.println("面積爲" + area);
// 以下這種方式和上面兩行代碼所表示的意思一樣,只是更爲簡潔
// System.out.println("面積爲"+test.method());
}
public int method() {
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 8; j++) {
System.out.print("* ");
}
System.out.println();
}
return 10 * 8;
}
}
第三問:
public class Test {
public static void main(String[] args) {
Test test = new Test();
int area = test.method(12, 11);
System.out.println("面積爲" + area);
// 以下這種方式和上面兩行代碼所表示的意思一樣,只是更爲簡潔
// System.out.println("面積爲"+test.method(11,10));
}
public int method(int m, int n) {
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
System.out.print("* ");
}
System.out.println();
}
return m * n;
}
}
練習四:對象數組
定義類Student,包含三個屬性:學號number(int),年級state(int),成績score(int)。
創建20個學生對象,學號爲1到20,年級和成績都由隨機數確定。
問題一:打印出3年級(state值爲3)的學生信息。
問題二:使用冒泡排序按學生成績排序,並遍歷所有學生信息
提示:
- 生成隨機數:Math.random(),返回值類型double;
- 四捨五入取整:Math.round(double d),返回值類型long。
先按題中所給出的條件寫出代碼
方法一
public class Test {
public static void main(String[] args) {
// 聲明Student類型的數組
Student[] stus = new Student[20]; // 都是對象數組 String[] arr = new String[10];
for (int i = 0; i < stus.length; i++) {
stus[i] = new Student();// 給數組元素賦值
stus[i].number = (i + 1);// 給Student對象的屬性賦值
stus[i].state = (int) (Math.random() * (6 - 1 + 1) + 1);// 年級:[1,6]
stus[i].score = (int) (Math.random() * (100 - 0 + 1));// 成績:[0,100]
}
// 這裏可以遍歷學生數組
for (int j = 0; j < stus.length; j++) {
System.out.println(stus[j].info());// 在這裏調用顯示學生信息的方法
}
}
}
class Student {
int number;// 學號
int state;// 年級
int score;// 成績
// 顯示學生信息的方法
public String info() {
return "學號: " + number + " , " + state + "年級" + ",成績:" + score + "分";
}
}
問題一:
public class Test {
public static void main(String[] args) {
// 聲明Student類型的數組
Student[] stus = new Student[20]; // 都是對象數組 String[] arr = new String[10];
for (int i = 0; i < stus.length; i++) {
stus[i] = new Student();// 給數組元素賦值
stus[i].number = (i + 1);// 給Student對象的屬性賦值
stus[i].state = (int) (Math.random() * (6 - 1 + 1) + 1);// 年級:[1,6]
stus[i].score = (int) (Math.random() * (100 - 0 + 1));// 成績:[0,100]
}
// 遍歷學生數組
for (int i = 0; i < stus.length; i++) {
// System.out.println(stus[i].number + "," + stus[i].state
// + "," + stus[i].score);
System.out.println(stus[i].info());
}
System.out.println("********************");
// 問題一:打印出3年級(state值爲3)的學生信息。
for (int i = 0; i < stus.length; i++) {
if (stus[i].state == 3) {
System.out.println(stus[i].info());
}
}
// 遍歷學生數組
for (int i = 0; i < stus.length; i++) {
System.out.println(stus[i].info());
}
}
}
class Student {
int number;// 學號
int state;// 年級
int score;// 成績
// 顯示學生信息的方法
public String info() {
return "學號:" + number + ",年級:" + state + ",成績:" + score;
}
}
第二問;
public class Test {
public static void main(String[] args) {
// 聲明Student類型的數組
Student[] stus = new Student[20]; // 都是對象數組 String[] arr = new String[10];
for (int i = 0; i < stus.length; i++) {
stus[i] = new Student();// 給數組元素賦值
stus[i].number = (i + 1);// 給Student對象的屬性賦值
stus[i].state = (int) (Math.random() * (6 - 1 + 1) + 1);// 年級:[1,6]
stus[i].score = (int) (Math.random() * (100 - 0 + 1));// 成績:[0,100]
}
// 遍歷學生數組
for (int i = 0; i < stus.length; i++) {
// System.out.println(stus[i].number + "," + stus[i].state
// + "," + stus[i].score);
System.out.println(stus[i].info());
}
// 遍歷學生數組
for (int i = 0; i < stus.length; i++) {
System.out.println(stus[i].info());
}
}
}
class Student {
int number;// 學號
int state;// 年級
int score;// 成績
// 顯示學生信息的方法
public String info() {
return "學號:" + number + ",年級:" + state + ",成績:" + score;
}
}