0x01
编写类A01,定义方法max,实现求某个double数组的最大值,并返回Homework01.java
public class Homework01 {
public static void main(String[] args){
double[] numbers = {
534, 1,34, 90, 56.9, 23, 45, 103.981};
A01 a01 = new A01();
System.out.println("The max number is " + a01.max(numbers));;
}
}
class A01{
public double max(double[] number){
double maxnum = number[0];
int i = 0;
for(; i < number.length; i++){
maxnum = maxnum > number[i] ? maxnum : number[i];
}
return maxnum;
}
}
The max number is 534.0
0x02
编写类A02,定义方法find,实现查找某字符串数组中的元素查找,并返回索引,如果找不到,返回-1。
public class Homework02 {
public static void main(String[] args){
String[] data = {
"hello", "hi", "yes", "no"};
String finddata = "yes";
A02 a02 = new A02();
int index = a02.find(data, finddata);
if(index == -1){
System.out.println("NoFind, the index is " + index);
}else {
System.out.println("Find, the index is " + index);
}
}
}
class A02{
public int find(String[] data, String finddata){
int i = 0;
for(; i < data.length; i++){
if(data[i].equals(finddata)){
return i;
}
}
return -1;
}
}
Find, the index is 2
0x03
编写类Book,定义方法updatePrice,实现更改某本书的价格,具体:如果价格>150,则更改为150,如果价格>100,更改为100,否则不变。
public class Homework03 {
public static void main(String[] args){
book bo = new book("xiyouji", 102.4);
bo.updatePrice();
}
}
class book{
String name;
double price;
public book(String name, double price){
this.name = name;
this.price = price;
}
public void updatePrice(){
if(price > 150)