- 1
public class Homework01 {
//编写一个main方法
public static void main(String[] args) {
A01 a01 = new A01();
double[] arr = {1, 1.4, -1.3, 89.8, 123.8 , 66}; //;{};
Double res = a01.max(arr);
if(res != null) {
System.out.println("arr的最大值=" + res);
} else {
System.out.println("arr的输入有误, 数组不能为null, 或者{}");
}
}
}
/*
编写类A01,定义方法max,实现求某个double数组的最大值,并返回
思路分析
1. 类名 A01
2. 方法名 max
3. 形参 (double[])
4. 返回值 double
先完成正常业务,然后再考虑代码健壮性
*/
class A01 {
public Double max(double[] arr) {
//老韩先判断arr是否为null,然后再判断 length 是否>0
if( arr!= null && arr.length > 0 ) {
//保证arr至少有一个元素
double max = arr[0];//假定第一个元素就是最大值
for(int i = 1; i < arr.length; i++) {
if(max < arr[i]) {
max = arr[i];
}
}
return max;//double
} else {
return null;
}
}
}
- 2
public class Homework02 {
//编写一个main方法
public static void main(String[] args) {
String[] strs = {"jack", "tom", "mary","milan"};
A02 a02 = new A02();
int index = a02.find("milan", strs);
System.out.println("查找的index=" + index);
}
}
//编写类A02,定义方法find,实现查找某字符串是否在字符串数组中,
//并返回索引,如果找不到,返回-1
//分析
//1. 类名 A02
//2. 方法名 find
//3. 返回值 int
//4. 形参 (String , String[])
//
//自己补充代码健壮性
class A02 {
public int find(String findStr, String[] strs) {
//直接遍历字符串数组,如果找到,则返回索引
for(int i = 0; i < strs.length; i++) {
if(findStr.equals(strs[i])) {
return i;
}
}
//如果没有,就返回-1
return -1;
}
}
- 3
public class Homework03 {
//编写一个main方法
public static void main(String[] args) {
//测试
Book book = new Book("笑傲江湖", 300);
book.info();
book.updatePrice();//更新价格
book.info();
}
}
/*
编写类Book, 定义方法updatePrice,实现更改某本书的价格,
具体:如果价格>150,则更改为150,如果价格>100,更改为100,否则不变
分析
1. 类名 Book
2. 属性 price, name
3. 方法名 updatePrice
4. 形参 ()
5. 返回值 void
6. 提供一个构造器
*/
class Book {
String name;
double price;
public Book(String name, double price) {
this.name = name;
this.price = price;
}
public void updatePrice() {
//如果方法中,没有 price 局部变量, this.price 等价 price
if(price > 150) {
price = 150;
} else if(price > 100 ) {
price = 100;
}
}
//显示书籍情况
public void info() {
System.out.println("书名=" + this.name + " 价格=" + this.price);
}
}
- 4
public class Homework04 {
//编写一个main方法
public static void main(String[] args) {
int[] oldArr = {10, 30, 50};
A03 a03 = new A03();
int[] newArr = a03.copyArr(oldArr);
//遍历newArr,验证
System.out.println("==返回的newArr元素情况==");
for(int i = 0; i < newArr.length; i++) {
System.out.print(newArr[i] + "\t");
}
}
}
/*
编写类A03, 实现数组的复制功能copyArr,输入旧数组,返回一个新数组,元素和旧数组一样
*/
class A03 {
public int[] copyArr(int[] oldArr) {
//在堆中,创建一个长度为 oldArr.length 数组
int[] newArr = new int[oldArr.length];
//遍历 oldArr,将元素拷贝到 newArr
for(int i = 0; i < oldArr.length; i++) {
newArr[i] = oldArr[i];
}
return newArr;
}
}
- 5
public class Homework05 {
//编写一个main方法
public static void main(String[] args) {
Circle circle = new Circle(3);
System.out.println("面积=" + circle.area());
System.out.println("周长=" + circle.len());
}
}
/*
定义一个圆类Circle, 定义属性:半径,提供显示圆周长功能的方法, 提供显示圆面积的方法
*/
class Circle {
double radius;
public Circle(double radius) {
this.radius = radius;
}
public double area() { //面积
return Math.PI * radius * radius;
}
public double len() { //周长
return 2 * Math.PI * radius;
}
}
- 6
public class Homework06 {
//编写一个main方法
public static void main(String[] args) {
Cale cale = new Cale(2, 10);
System.out.println("和=" + cale.sum());
System.out.println("差=" + cale.minus());
System.out.println("乘=" + cale.mul());
Double divRes = cale.div();
if(divRes != null) {
System.out.println("除=" + divRes);
}
}
}
/*
编程创建一个Cale计算类,在其中定义2个变量表示两个操作数,
定义四个方法实现求和、差、乘、商(要求除数为0的话,要提示) 并创建两个对象,分别测试
*/
class Cale {
double num1;
double num2;
public Cale(double num1, double num2) {
this.num1 = num1;
this.num2 = num2;
}
//和
public double sum() {
return num1 + num2;
}
//差
public double minus() {
return num1 - num2;
}
//乘积
public double mul() {
return num1 * num2;
}
//除法
//
public Double div() {
//判断
if(num2 == 0) {
System.out.println("num2 不能为0");
return null;
} else {
return num1 / num2;
}
}
}
- 7
public class This01 {
//编写一个main方法
public static void main(String[] args) {
Dog dog1 = new Dog("大壮", 3);
System.out.println("dog1的hashcode=" + dog1.hashCode());
//dog1调用了 info()方法
dog1.info();
System.out.println("============");
Dog dog2 = new Dog("大黄", 2);
System.out.println("dog2的hashcode=" + dog2.hashCode());
dog2.info();
}
}
class Dog{ //类
String name;
int age;
// public Dog(String dName, int dAge){//构造器
// name = dName;
// age = dAge;
// }
//如果我们构造器的形参,能够直接写成属性名,就更好了
//但是出现了一个问题,根据变量的作用域原则
//构造器的name 是局部变量,而不是属性
//构造器的age 是局部变量,而不是属性
//==> 引出this关键字来解决
public Dog(String name, int age){//构造器
//this.name 就是当前对象的属性name
this.name = name;
//this.age 就是当前对象的属性age
this.age = age;
System.out.println("this.hashCode=" + this.hashCode());
}
public void info(){//成员方法,输出属性x信息
System.out.println("this.hashCode=" + this.hashCode());
System.out.println(name + "\t" + age + "\t");
}
}
- 8
先调用count1,结果为10,
然后再进入count2.count2先进入后自加。
结果为9然后为10
- 9
public class Homework09 {
//编写一个main方法
public static void main(String[] args) {
Music music = new Music("笑傲江湖", 300);
music.play();
System.out.println(music.getInfo());
}
}
/*
义Music类,里面有音乐名name、音乐时长times属性,
并有播放play功能和返回本身属性信息的功能方法getInfo
*/
class Music {
String name;
int times;
public Music(String name, int times) {
this.name = name;
this.times = times;
}
//播放play功能
public void play() {
System.out.println("音乐 " + name + " 正在播放中.... 时长为" + times + "秒");
}
//返回本身属性信息的功能方法getInfo
public String getInfo() {
return "音乐 " + name + " 播放时间为" + times;
}
}
- 10
d1,d2指向内存中的同一块区域。
则d1的变化会影响d2
- 11
每个函数里面都含有两个参数。然后两个参数进行递归调用
- 12
public class Homework12 {
//编写一个main方法
public static void main(String[] args) {
}
}
/*
创建一个Employee类,
属性有(名字,性别,年龄,职位,薪水), 提供3个构造方法,可以初始化
(1) (名字,性别,年龄,职位,薪水),
(2) (名字,性别,年龄) (3) (职位,薪水), 要求充分复用构造器
*/
class Employee {
//名字,性别,年龄,职位,薪水
String name;
char gender;
int age;
String job;
double sal;
//因为要求可以复用构造器,因此老韩先写属性少的构造器
//职位,薪水
public Employee(String job, double sal) {
this.job = job;
this.sal = sal;
}
//名字,性别,年龄
public Employee(String name, char gender, int age) {
this.name = name;
this.gender = gender;
this.age = age;
}
//名字,性别,年龄,职位,薪水
public Employee(String job, double sal, String name, char gender, int age) {
this(name, gender, age);//使用到 前面的 构造器
this.job = job;
this.sal = sal;
}
}
- 13
/*
题目要求:
(1) 定义一个Circle类,包含一个double型的radius属性代表圆的半径,findArea()方法返回圆的面积。
(2) 定义一个类PassObject,在类中定义一个方法printAreas(),该方法的定义如下:
public void printAreas(Circle c, int times) //方法签名/声明
(3) 在printAreas方法中打印输出1到times之间的每个整数半径值,以及对应的面积。例如,times为5,则输出半径1,2,3,4,5,以及对应的圆面积。
(4) 在main方法中调用printAreas()方法,调用完毕后输出当前半径值。程序运行结果如图所示
*/
public class Homework13 {
//编写一个main方法
public static void main(String[] args) {
Circle c = new Circle();
PassObject po = new PassObject();
po.printAreas(c, 5);
}
}
class Circle { //类
double radius;//半径
public Circle() { //无参构造器
}
public Circle(double radius) {
this.radius = radius;
}
public double findArea() {//返回面积
return Math.PI * radius * radius;
}
//添加方法setRadius, 修改对象的半径值
public void setRadius(double radius) {
this.radius = radius;
}
}
class PassObject {
public void printAreas(Circle c, int times) {
System.out.println("radius\tarea");
for(int i = 1; i <= times; i++) {//输出1到times之间的每个整数半径值
c.setRadius(i) ; //修改c 对象的半径值
System.out.println((double)i + "\t" + c.findArea());
}
}
}