本章练习
以下代码均已编译通过
public class Homework01 {
public static void main(String[] args) {
double[] arr = {2.45, 45.4, -4324.4, 434.7, 2.76};
A01 m = new A01();
Double ret = m.max(arr);
if(ret != null){
System.out.println("最大值为:"+ret);
}else{
System.out.println("Wrong input!");
}
}
}
//完成基本功能后,再考虑健壮性
class A01{
// 包装类 Double
public Double max(double[] arr){
// 先判不为空,再判长度大于零
if(arr != null && arr.length > 0){
double max = arr[0]; // 假定第一个数就是最大
for (int i = 1; i < arr.length; i++) {
if(arr[i] > max){
max = arr[i];
}
}
return max;
}else{
return null;
}
}
}
public class Homework02 {
public static void main(String[] args) {
A02 p = new A02();
String[] arr = {"Tim", "Jack", "Mary"};
System.out.println(p.find(arr, "Jack"));
System.out.println(p.find(arr, "Java"));
}
}
class A02 {
public int find(String[] arr, String partten){
if(arr != null && arr.length > 0){
for(int i = 0; i < arr.length; i++){
if(partten.equals(arr[i])){
return i;
}
}
}
return -1;
}
}
public class Homework03 {
public static void main(String[] args) {
Book b1 = new Book("三体", 120.99);
b1.info();
b1.updatePrice();
b1.info();
}
}
class Book {
String name;
double price;
public Book(String name, double price){
this.name = name;
this.price = price;
}
public void updatePrice(){
if(this.price > 150){
this.price = 150;
}else if(this.price > 100){
this.price = 100;
}
}
public void info(){
System.out.println(this.name + "的价格为:" + this.price);
}
}
public class Homework04 {
public static void main(String[] args) {
int[] arr = {1, 4, 6, 2, 3};
A03 a = new A03();
int[] newArr = a.copyArr(arr);
for (int i = 0; i < newArr.length; i++) {
System.out.print(newArr[i] + "\t");
}
}
}
class A03 {
public int[] copyArr(int[] oldArr){
// 在堆中创建一个长度为 oldArr.length 的数组
int[] newArr = new int[oldArr.length];
for(int i = 0; i < oldArr.length; i++){
newArr[i] = oldArr[i];
}
return newArr;
}
}
public class Homework05 {
public static void main(String[] args) {
Circle circle = new Circle(3);
System.out.printf("周长为:%.2f\n", circle.perimeter());
System.out.printf("面积为:%.2f", circle.area());
}
}
class Circle {
double radius;
public Circle(double radius){
this.radius = radius;
}
// 周长
public double perimeter(){
return 2.0 * this.radius * Math.PI;
}
// 面积
public double area(){
return Math.PI * this.radius * this.radius;
}
}
public class Homework06 {
public static void main(String[] args) {
Cale cale = new Cale(20, 4);
System.out.println(cale.add());
System.out.println(cale.sub());
System.out.println(cale.mul());
if(cale.div() != null){
System.out.println(cale.div());
}
Cale cale1 = new Cale(20, 0);
if(cale1.div() != null){
System.out.println(cale1.div());
}
}
}
class Cale {
double op1, op2;
public Cale(double op1, double op2){
this.op1 = op1;
this.op2 = op2;
}
double add(){
return op1 + op2;
}
double sub(){
return op1 - op2;
}
double mul(){
return op1 * op2;
}
Double div(){
if(op2 == 0){
System.out.println("除数不能为零");
return null;
}else{
return op1 / op2;
}
}
}
public class Homework07 {
public static void main(String[] args) {
Dog dog = new Dog("大黄", "White", 4);
dog.show();
}
}
class Dog {
String name;
String color;
int age;
public Dog(String name, String color, int age){
this.name = name;
this.color = color;
this.age = age;
}
public void show(){
System.out.println("名字=" + this.name +
" 颜色=" + this.color+
" 年龄=" + this.age);
}
}
public class Homework07 {
public static void main(String[] args) {
Dog dog = new Dog("大黄", "White", 4);
dog.show();
}
}
class Dog {
String name;
String color;
int age;
public Dog(String name, String color, int age){
this.name = name;
this.color = color;
this.age = age;
}
public void show(){
System.out.println("名字=" + this.name +
" 颜色=" + this.color+
" 年龄=" + this.age);
}
}
9.略
10.输出为:
i=101
j=100
101
101
public double method(double n1, double n2){...}
public class Homework12 {
public static void main(String[] args) {
Emploee emploee = new Emploee("Tim", "male", 20);
System.out.println(emploee.name + "\t" +
emploee.gender + "\t" + emploee.age);
}
}
class Emploee {
String name;
String gender;
int age;
String position;
int salary;
public Emploee(String position, int salary){
this.position = position;
this.salary = salary;
}
public Emploee(String name, String gender, int age){
this.name = name;
this.gender = gender;
this.age = age;
}
public Emploee(String name, String gender, int age, String position, int salary) {
this(name, gender, age); // 复用第二个构造器,注意不能同时复用前两个
this.position = position;
this.salary = salary;
}
}
public class Homework13 {
public static void main(String[] args) {
new PassObject().printAreas(5);
}
}
class Circle {
double radius;
public Circle(double radius){
this.radius = radius;
}
public double findArea(){
return radius * radius * Math.PI;
}
}
class PassObject {
public void printAreas(int times){
System.out.println("Radius" + "\t" + "Area");
for(double i = 1.0; i <= times; i++){
System.out.print(i + "\t");
System.out.print(new Circle(i).findArea());
System.out.println();
}
}
}
import java.util.Random;
import java.util.Scanner;
import javax.lang.model.util.ElementScanner6;
public class Homework14 {
public static void main(String[] args) {
// 创建扫描器接收数据
Scanner scanner = new Scanner(System.in);
Tom tom = new Tom();
System.out.println("输入你想玩的局数:");
int countTotal = scanner.nextInt();
// 循环 countToatal 次
for(int i = 1; i <= countTotal; i++){
System.out.println("请出拳: 0-拳头 1-石头 2-剪刀 ");
tom.tomGuessNum = scanner.nextInt();
tom.setTomGuessNum(tom.tomGuessNum);
tom.comGuessNum = tom.computerNum();
String outcome = tom.vsComputer();
tom.winCountNum = tom.winCount(outcome);
tom.showInfo(i, outcome);
}
System.out.println("你总共赢了:" + tom.winCountNum + "局");
scanner.close();
}
}
class Tom{
int tomGuessNum;
int comGuessNum;
int winCountNum;
// 显示当前局信息
public void showInfo(int count, String outcome){
System.out.println("==================================================================");
System.out.println("当前局数\t你的出拳\t电脑出拳\t结果");
System.out.println(count + "\t\t" + tomGuessNum + "\t\t"
+ comGuessNum + "\t\t" + outcome);
System.out.println("==================================================================");
}
public int computerNum(){
Random r = new Random();
return r.nextInt(3); // 0-2的随机数
}
public void setTomGuessNum(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
|| tomGuessNum ==1 && comGuessNum == 2
|| tomGuessNum == 2 && comGuessNum == 0){
return "你赢了";
}else if(tomGuessNum == comGuessNum){
return "平局";
}else{
return "你输了";
}
}
public int winCount(String s){
if(s.equals("你赢了")){
winCountNum++;
}
return winCountNum;
}
}
评论