编写类A01,定义方法max,实现求某个double数组的最大值,并返回
public class Homework01 { public static void main(String[] args) { //编写类A01,定义方法max,实现求某个double数组的最大值,并返回 A01 a01 = new A01(); Double arr[] = {1.0, 4.7, 1.8}; Double res = a01.max(arr); if (res != null){ System.out.println("arr数组的最大值 " + a01.max(arr)); } else { System.out.println("arr的输入有误"); } } } class A01 { public Double max(Double[] arr) { if (arr.length > 0) { double max = arr[0]; for (int i = 0; i < arr.length; i++) { if (max < arr[i]) { max = arr[i]; } } return max; } return null; } }案例 2
编写类A02,定义方法find,实现查找某字符串是否在字符串数组中,并返回索引如果找不到,返回-1
public class Homework02 { public static void main(String[] args) { //编写类A02,定义方法find,实现查找某字符串是否在字符串数组中,并返回索引如果找不到,返回-1 //1.类名:A02 //2.方法名 find //3.返回值:int //4.形参:String String[] String[] strs = null; A02 a02 = new A02(); int index = a02.find("jack", strs); System.out.println("查找的index=" + index); } } class A02 { public int find(String findStr, String[] strs) { if (strs != null && strs.length > 0) { for (int i = 0; i < strs.length; i++) { if (findStr.equals(strs[i])) { return i; } } } return -1; } }案例 3
编写类Book,定义方法updatePrice,实现更改某本书的价格,具体:如果价格>150,则更改为150,如果价格>100,更改为100,否则不变
public class HomeworkO3 { //编写类Book,定义方法updatePrice,实现更改某本书的价格, // 具体:如果价格>150,则更改为150,如果价格>100,更改为100,否则不变 //分析: //1.类名:Book //2.属性:name,price //3.方法名:updatePrice //4.形参:() //5.返回值:void //6.提供一个构造器 public static void main(String[] args) { Book book = new Book("笑傲江湖", 178); book.info(); book.updatePrice(); book.info(); } } class Book{ String name; double price; public Book() { } public Book(String name, double price) { this.name = name; this.price = price; } public void updatePrice(){ if (price>150){ price=150; }else if (price>100){ price=100; } } public void info(){ System.out.println("书名= "+name+"价格="+price); } }案例 4
编写类AO3,实现数组的复制功能copyArr,输入旧数组,返回一个新数组,元素和旧数组一样 Homework04.java
public class Homework04 { public static void main(String[] args) { int oldArr[] ={10,20,30}; A03 a03 = new A03(); int newArr[] = a03.copyArr(oldArr); System.out.println("=========返回的newArr元素情况==========="); for (int i = 0; i < newArr.length ; i++) { System.out.print(newArr[i]+" "); } } } class A03 { public int[] copyArr(int oldArr[]) { int newArr[] =new int[oldArr.length]; for (int i = 0; i < oldArr.length; i++) { newArr[i] = oldArr[i]; } return newArr; } }案例 5
定义一个圆类Circle,定义属性:半径,提供显示圆周长功能的方法,提供显示圆面积的方法 HomeworkO5.java
public class HomeWork05 { public static void main(String[] args) { //定义一个圆类Circle,定义属性:半径,提供显示圆周长功能的方法,提供显示圆面积的方法 Circle circle = new Circle(3); System.out.println("周长="+circle.len()); System.out.println("面积="+circle.area()); } } 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
编程创建一个Cale计算类,在其中定义2个变量表示两个操作数,定义四个方法实现求和、差、乘、商(要求除数为0的话,要提示)并创建两个对象,分别测试
public class Homework06 { public static void main(String[] args) { Cale cale = new Cale(1,5); 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("除="+cale.div()); } } } 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
定义Music类,里面有音乐名name、音乐时长times属性,并有播放play功能和返回本身属性信息的功能方法getlnfo
Homework07.java
public class Homework07 { public static void main(String[] args) { Music music = new Music("笑傲江湖", 300); music.play(); System.out.println(music.getInfo()); } } class Music { String name; int times; public Music(String name, int times) { this.name = name; this.times = times; } public void play() { System.out.println("音乐 " + name + "正在播放中......"); } public String getInfo() { return "音乐 " + name + "播放时长为 " + times; } }案例 8
创建一个Employee类,属性有((名字,性别,年龄,职位,薪水),提供3个构造方法,可以初始化
(1)(名字,性别,年龄,职位,薪水)
(2)(名字,性别,年龄)
(3)(职位,薪水)
要求充分复用构造器
Homework08.java
public class Homework08 { public static void main(String[] args) { } } class Employee { String name; String gender; String age; String job; String sal; public Employee(String job, String sal) { this.job = job; this.sal = sal; } public Employee(String name, String gender, String age) { this.name = name; this.gender = gender; this.age = age; } public Employee(String name, String gender, String age, String job, String sal) { this(name, gender, age); this.job = job; this.sal = sal; } }案例 9
将对象作为参数传递给方法。Homework09.java
题目要求:
(1)定义一个Circle类,包含一个double型的radius属性代表圆的半径,findArea()方法返回圆的面积。
(2)定义一个类PassObject,在类中定义一个方法printAreas(),该方法的定义如下:
public void printAreas(Circle c, int times)1/方法签名/声明
(3)在printAreas方法中打印输出1到times之间的每个整数半径值,以及对应的面积。例如,times为 5,则输出半径1,2,3,4,5,以及对应的圆面积。
(4)在main方法中调用printAreas()方法,调用完毕后输出当前半径值。程序运行结果如图所示
public class Homework09 { public static void main(String[] args) { PassObject po = new PassObject(); po.printAreas(new Circle(),5); } } class Circle { double radius; public Circle() { } public Circle(double radius) { this.radius = radius; } public double findArea() { return Math.PI * radius * radius; } public void setRadius(double radius) { this.radius = radius; } } class PassObject { public void printAreas(Circle c, int times) { System.out.println("radiustarea"); for (int i = 1; i <= times; i++) { c.setRadius(i); System.out.println((double)i+" t"+c.findArea()); } } }案例 10
有个人Tom设计他的成员变量.成员方法,可以电脑猜拳,电脑每次都会随机生成0,1,2
0表示石头1表示剪刀2表示布并要可以显示Tom的输赢次数(清单)
public class MoraGame { public static void main(String[] args) { //创建一个玩家对象 Tom t = new Tom(); //用来记录最后输赢的次数 int isWinCount = 0; //创建一个二维数组,用来接收局数,Tom出拳情况以及电脑出拳情况 int[][] arr1 = new int[3][3]; int j = 0; //创建一个一维数组,用来接收输赢情况 String[] arr2 = new String[3]; Scanner scanner = new Scanner(System.in); for (int i = 0; i < 3; i++) { //获取玩家出的拳 System.out.println("请输入你要出的拳(0-拳头,1-剪刀,2-布) :"); int num = scanner.nextInt(); t.setTomGussNum(num); int tomGuess = t.getTomGuessNum(); arr1[i][j + 1] = tomGuess; //获取电脑出的拳 int comGuess = t.computerNum(); arr1[i][j + 2] = comGuess; //将玩家猜的拳头与电脑做比较 String isWin = t.vsComputer(); arr2[i] = isWin; arr1[i][j] = t.count; //对每一局情况进行输出 System.out.println("==================================="); System.out.println("局数t玩家的出拳t电脑的出拳t输赢情况"); System.out.println(t.count + " t" + tomGuess + " t" + "tt" + comGuess + " ttt" + isWin); System.out.println("==================================="); System.out.println("nn"); isWinCount = t.winCount(isWin); } //对游戏最终结果进行输出 System.out.println("局数t玩家的出拳t电脑的出拳t输赢情况"); for (int a = 0; a < arr1.length; a++) { for (int b = 0; b < arr1[a].length; b++) { System.out.print(arr1[a][b] + "ttt"); } System.out.println(arr2[a]); System.out.println(); } System.out.println("你赢了" + isWinCount + "次"); } } class Tom { //玩家出拳的类型 int tomGuessNum; //0,1, 2 //电脑出拳的类型 int comGuessNum; //0,1, 2 //玩家赢的次数 int winCountNum; //比赛的次数 int count = 1; //一共比赛3次 public void showInfo() { //... } public int computerNum() { Random r = new Random(); comGuessNum = r.nextInt(3); // 方法 返回0-2的随机数 return comGuessNum; } public void setTomGussNum(int tomGuessNum) { if (tomGuessNum > 2 || tomGuessNum < 0) { throw new IllegalArgumentException("数字输入错误"); } this.tomGuessNum = tomGuessNum; } public int getTomGuessNum() { return tomGuessNum; } public String vsComputer() { if (tomGuessNum == 0 && comGuessNum == 1) { return "你赢了"; } else if (tomGuessNum == 1 && comGuessNum == 2) { return "你赢了"; } else if (tomGuessNum == 2 && comGuessNum == 0) { return "你赢了"; } else if (tomGuessNum == comGuessNum) { return "平手"; } else { return "你输了"; } } public int winCount(String s) { count++;//控制玩的次数 if (s.equals("你赢了")) { //统计赢的次数 winCountNum++; } return winCountNum; } }