- 练习:
- 利用面向对象的编程方法,设计类Ciecle计算圆的面积 CircleTest.Java
- ExerTest.java 3.1 编写程序, 声明一个method方法, 在方法中打印一-个108的型矩形,在main方法 中调用该方法。 * 3.2 修改上一个程序, 在method方法中,除打印一-个10* 8的型矩形外, 再计算该矩形的面积, * 并将其作为方法返回值。 在main方法中调用该方法,接收返回的面积值并打印。 * 3.3 修改上一个程序, 在method方法提供m和n两个参数, 方法中打印一一个m n的*型矩形, * 并计算该矩形的面积, 将其作为方法返回值。在main方法中调用该方法,接收返回的面积值并打印
- 定义类Student,包含三个属性:学号number(int),年级state(int),成绩score(int)。创建20个学生对象, 学号为1到20,年级和成绩都由随机数确定问题一:打印出3年级(state值为3)的学生信息。只问题二:使用冒泡排序按学生成绩排序,并遍历所有学生信息提示:1)生成随机数: Math.random(), 返回值类型double;2)四舍五入取整: Math.round(double d), 返回值类型long:5.声明一个日期类型MyDate:有属性:年year,月 month,日day。 创建2个E对象,分别赋佰为,你的出牛日期,你对象的出牛日期,并显示信息。StudentTest.java
package com.atguigu.com;
/*
* 3.1 编写程序, 声明一个method方法, 在方法中打印一-个10*8的*型矩形,在main方法 中调用该方法。
* 3.2 修改上一个程序, 在method方法中,除打印一-个10* 8的*型矩形外, 再计算该矩形的面积,
* 并将其作为方法返回值。 在main方法中调用该方法,接收返回的面积值并打印。
* 3.3 修改上一个程序, 在method方法提供m和n两个参数, 方法中打印一一个m* n的*型矩形,
* 并计算该矩形的面积, 将其作为方法返回值。在main方法中调用该方法,接收返回的面积值并打印。
*/
public class ExerTest {
public static void main(String[] args) {
ExerTest test = new ExerTest(); //因为method定义在exertest类中,必须要创建一个对象,来调用method。
//3.1测试
// test.method();
//3.2
//方式一:
// int area = test.method();
// System.out.println("此矩阵的面积为:" + area);
//
//方式二:
// System.out.println(test.method());
//3.3
int area = test.method(5, 4);
System.out.println("矩阵的面积为:" + area);
}
//3.1
// public void method() {
// for(int i = 0; i < 10; i++) {
// for(int j = 0; j < 8; j++) {
// System.out.print("* ");
// }
// System.out.println();
// }
// }
// 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;
// }
//3.3
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;
}
}
package com.atguigu.com;
/*
* 定义类Student,包含三个属性:学号number(int),年级state(int),成绩score(int)。
* 创建20个学生对象, 学号为1到20,年级和成绩都由随机数确定问题一:
* 打印出3年级(state值为3)的学生信息。
只问题二:使用冒泡排序按学生成绩排序,并遍历所有学生信息
提示:
1)生成随机数: Math.random(), 返回值类型double;
2)四舍五入取整: Math.round(double d), 返回值类型long:
5.声明一个日期类型MyDate:有属性:年year,月 month,日day。 创建2个E对象,分别赋佰为,你的出牛日期,你对象的出牛日期,并显示信息。
*
*
*
*
*/
public class StudentTest {
public static void main(String[] args) {
//声明Student类型的数组
Student[] stus = new Student[20];
//遍历数组
for(int i = 0; i <stus.length; i++) {
//给数组元素赋值
stus[i] = new Student();
//给Student对象的属性赋值
stus[i].number = i + 1;
//年级:[1,6]
stus[i].state = (int)(Math.random() * (6 - 1 + 1) + 1);
//成绩
stus[i].score = (int)(Math.random() * (100 - 0 + 1) + 0);
}
//遍历一下学生数组
for(int i = 0 ; i < stus.length; i++) { //new 过对象了,会把对象的地址值给stus[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++) {
for(int j = 0; j < stus.length - 1 - i; j++) {
//现在要交换的时学生,而不是交换成绩
if(stus[j].score > stus[j + 1].score) {
Student temp = stus[j];
stus[j] = stus[j + 1];
stus[j + 1] = temp;
}
}
}
System.out.println("********************************");
for(int i = 0 ; i < stus.length; i++) { //new 过对象了,会把对象的地址值给stus[i]
System.out.println(stus[i].info());
}
}
}
class Student{
//属性
int number; //学号
int state; //年纪
int score; //成绩
//显示学生信息的方法
public String info(){
return "学号:" + number + ",年级:" + state + ",成绩:" + score;
}
}
package com.atguigu.com;
/*
* 定义类Student,包含三个属性:学号number(int),年级state(int),成绩score(int)。
* 创建20个学生对象, 学号为1到20,年级和成绩都由随机数确定问题一:
* 打印出3年级(state值为3)的学生信息。
只问题二:使用冒泡排序按学生成绩排序,并遍历所有学生信息
提示:
1)生成随机数: Math.random(), 返回值类型double;
2)四舍五入取整: Math.round(double d), 返回值类型long:
5.声明一个日期类型MyDate:有属性:年year,月 month,日day。 创建2个E对象,分别赋佰为,你的出牛日期,你对象的出牛日期,并显示信息。
*
*
* 此代码时对StudentTest.java的改进。将操作数组的功能封装到方法中
*
*/
public class StudentTest1 {
public static void main(String[] args) {
//声明Student类型的数组
Student1[] stus = new Student1[20];
//遍历数组
for(int i = 0; i <stus.length; i++) {
//给数组元素赋值
stus[i] = new Student1();
//给Student对象的属性赋值
stus[i].number = i + 1;
//年级:[1,6]
stus[i].state = (int)(Math.random() * (6 - 1 + 1) + 1);
//成绩
stus[i].score = (int)(Math.random() * (100 - 0 + 1) + 0);
}
StudentTest1 test = new StudentTest1();
//遍历一下学生数组
test.print(stus);
System.out.println("********************************");
//问题一:打印出3年级(state值为3)的学生信息。
test.scearchStste(stus, 3);
//问题三:使用冒泡排序按学生成绩排序,并遍历所有学生信息
test.sort(stus);
System.out.println("********************************");
//遍历学生数组
test.print(stus);
}
/**
* 遍历Student1[]数组的操作
* @param stus
*/
public void print(Student1[] stus) {
//遍历一下学生数组
for(int i = 0 ; i < stus.length; i++) {
System.out.println(stus[i].info());
}
}
/**
*
* 查找Student数组中指定年级的学生信息
* @param stus 要查找的数组
* @param state 要找的年级
*/
public void scearchStste(Student1[] stus, int state) { //()中传入要给谁遍历,”遍历的值“
for(int i = 0; i < stus.length; i++) {
if(stus[i].state == 3) {
System.out.println(stus[i].info());
}
}
}
/**
* 给Student1数组排序
* @param stus 学生数组
*/
public void sort(Student1[] stus) { //()中显示,给谁排序,
for(int i = 0;i < stus.length; i++) {
for(int j = 0; j < stus.length - 1 - i; j++) {
//现在要交换的时学生,而不是交换成绩
if(stus[j].score > stus[j + 1].score) {
Student1 temp = stus[j];
stus[j] = stus[j + 1];
stus[j + 1] = temp;
}
}
}
}
}
class Student1{
//属性
int number; //学号
int state; //年纪
int score; //成绩
//显示学生信息的方法
public String info(){
return "学号:" + number + ",年级:" + state + ",成绩:" + score;
}
}
package com.atguigu.com;
/*
* 类中方法的声明于使用
*
* 方法:描述类应该有的功能
* 比如“ Math类,sqrt()//开方\\rangom\...
* Scanner类,nextXxxx()...
* Arrays类,sort()//排序\ birarySearch()//查找、 toString()//输出、 equals()//对比\
* 完成特定的事情
*
* 1.举例
* public void eat() { //无返回值
* public void sleep(int house) {//int house 是形参
* public String getName()//有返回值
* public String getNation(String nation) {
*
* 2方法的声明:一定有:权限修饰符,返回值类型,方法名(形参列表){
* 方法体
* }
* 3.权限修饰符:现在默认权限修饰符为:public
private,public,缺省,protected
* 权限修饰符
private,public,缺省,protected
返回值类型:有返回值,没有返回值
如果方法有返回值,则必修在方法声明时,指定返回时的类型。返回的数据必须是我定义的类型同时,在方法中需要使用return关键字;来返回指定类型的变量或常量
如果方法没有返回值,则方法声明是,使用void来表示,通常,没有返回值的,就不需要return,但是,如果使用的话,只能使用“return;”表示结束此方法的意思
该不该有返回值
具体问题具体分析
* 方法名:属于标识符,遵循标识符的规则和规范,“见名知意”
形参列表
方法可以声明0个,多个形参
格式:数据类型1 形参1,数据,数据类型2 形参2.....
用逗号来区分多个形参
我们定义方法时,该不该定义形参
具体情况具体分析
方法体
方法功能的体现,
return关键字的使用:
使用范围:使用在方法体中
作用
结束一个方法
针对于有返回值类型的方法,使用"return 数据"方法返回所要的数据
注意,return后面不可以有执行语句
方法的使用
*
*
*/
public class CustomerTest {
public static void main(String[] args) {
Customer cust1 = new Customer();
cust1.eat();
cust1.sleep(8);
}
}
class Customer{
//属性
String names;
int age;
boolean isMale;
//方法
public void eat() {
System.out.println("客户吃饭");
}
public void sleep(int house) {
System.out.println("休息了" + house + "个小时");
eat();
}
public String getName() {
return "Tom";
}
public String getNation(String nation) {
String info = "我的国际是:" + nation;
return info;
}
}