1.编写类A01,定义方法max,实现求某个double数组的最大值,并返回
public class homework01 {
public static void main(String[] args){
A01 a1 = new A01();
double[] arr = {23.4,12.1,1.4,67.8,34.7};
double max = a1.getMax(arr);
System.out.println("数组最大值为:"+max);
}
}
class A01{
public double getMax(double[] arr){
double max = arr[0];
for (int i =1; i < arr.length; i++){
if (max < arr[i]){
max = arr[i];
}
}
return max;
}
}
2.编写类A02,定义方法find,实现查找某字符串是否在字符串数组中,并返回索引,如果找不到,返回-1
import java.lang.module.FindException;
public class homework02 {
public static void main(String[] args){
A02 a1 = new A02();
String[] arr = {"tom","12.1","annna","world","hello"};
String str = "hell";
int index = a1.find(arr,str);
if (index == -1){
System.out.println("字符串"+str+"不在数组里");
}else {
System.out.println("字符串"+str+"在数组里第"+(index+1)+"位");
}
}
}
class A02{
public int find(String[] arr,String str){
int index = -1;
for (int i =0; i < arr.length; i++){
if (str.equals(arr[i])){
index = i;
}
}
return index;
}
}
3.编写类Book, 定义方法updatePrice,实现更改某本书的价格,具体:如果价格>150,则更改为150,如果价格>100,更改为100,否则不变
public class homework03 {
public static void main(String[] args){
Book book = new Book();
double price = 27;
double updateprice = book.updatePrice(price);
System.out.println("更新后的价格:"+updateprice);
}
}
class Book{
public double updatePrice(double price){
if(price > 150) {
price = 150;
} else if(price > 100 ) {
price = 100;
}
return price;
}
}
4.编写类A03, 实现数组的复制功能copyArr,输入旧数组,返回一个新数组,元素和旧数组一样
public class homework04 {
public static void main(String[] args){
A03 a03 = new A03();
double[] arr = {23.4,12.1,1.4,67.8,34.7};
double[] new_arr = a03.copyArr(arr);
System.out.println("更新后的数组为:");
for (int i = 0; i < new_arr.length; i++){
System.out.print(new_arr[i]+"\t");
}
}
}
class A03 {
public double[] copyArr(double[] arr) {
double[] new_arr = new double[arr.length];
for (int i = 0; i < arr.length; i++) {
new_arr[i] = arr[i];
}
return new_arr;
}
}
5.定义一个圆类Circle, 定义属性:半径,提供显示圆周长功能的方法, 提供显示圆面积的方法
import java.util.Scanner;
public class homework05 {
public static void main(String[] args) {
Circle circle = new Circle();
Scanner myscanner = new Scanner(System.in);
System.out.println("请输入要计算的圆的半径:");
circle.radius = myscanner.nextDouble();
double perimeter = circle.getPerimeter();
double area = circle.getArea();
System.out.println("圆的周长:" + perimeter);
System.out.println("圆的面积:" + area);
}
}
class Circle{
double radius;
final float pi=3.14f;//定义常量pi(圆周率)
public double getPerimeter(){
double perimeter = 2*pi*radius;
return perimeter;
}
public double getArea(){
double area = pi*radius*radius;
return area;
}
}
6. 编程创建一个Cale计算类,在其中定义2个变量表示两个操作数,定义四个方法实现求和、差、乘、商(要求除数为0的话,要提示) 并创建两个对象,分别测试
public class homework06 {
public static void main(String[] args) {
Cale cale = new Cale();
cale.number1 = 2;
cale.number2 = 3;
double add = cale.getAdd();
double sub = cale.getSubtract();
double mul = cale.getMultiply();
double divide = cale.getDivide();
System.out.println("加:" + add);
System.out.println("减:" + sub);
System.out.println("乘:" + mul);
System.out.println("除:" + divide);
}
}
class Cale{
double number1;
double number2;
public double getAdd(){
double add = number1 + number2;
return add;
}
public double getSubtract(){
double sub = number1 - number2;
return sub;
}
public double getMultiply(){
double mul = number1*number2;
return mul;
}
public double getDivide(){
double divide = 0;
if (number2 == 0){
System.out.println("除数为0,输入错误");
}
else {
divide = number1/number2;
}
return divide;
}
}
7.设计一个Dog类,有名字、颜色和年龄属性,定义输出方法show()显示其信息并创建对象,进行测试、【提示 this.属性,自己做】
public class homework07 {
public static void main(String[] args) {
Dog dog = new Dog("乐乐",10,"黄色");
dog.show();
}
}
class Dog{
String name;
int age;
String color;
public Dog(String name,int age,String color){
this.name = name;
this.age = age;
this.color = color;
}
public void show(){
System.out.println("名字:"+name+"\n年龄:"+age+"\n颜色:"+color);
}
}
8.给定一个Java程序的代码如下所示,则编译运行后,输出结果是
10,9,10
public class homework08 { //Test.java
int count = 9;
public void count1() {
count = 10;
System.out.println("count1=" + count);
}
public void count2() {
System.out.println("count1=" + count++);
}
public static void main(String args[]) {
new homework08().count1();
homework08 t1 = new homework08();
t1.count2();
t1.count2();
}
}
9.定Music类,里面有音乐名name、音乐时长times属性,并有播放play功能和返回本身属性信息的功能方法getInfo
public class homework09 {
public static void main(String[] args){
Music music = new Music("江南",27);
music.play();
String info = music.getInfo();
System.out.println(info);
}
}
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+"播放:"+times+"秒");
}
public String getInfo(){
String info = "音乐"+name+"播放:"+times+"秒";
return info;
}
}
10.试写出以下代码的运行结果
public class Homework10 {
//编写一个main方法
public static void main(String[] args) {
}
}
class Demo{
int i=100;
public void m(){
int j=i++;
System.out.println("i="+i);
System.out.println("j="+j);
}
}
class Test{
public static void main(String[] args){//运行它
Demo d1=new Demo();
Demo d2 = d1;
d2.m();
System.out.println(d1.i);
System.out.println(d2.i);
}
}
101 100 101 101
11.在测试方法中,调用method方法,代码如下,编译正确,试写出method方法的定义形式 ,调用语句为:System.out.println(method(method(10.0,20.0),100));
public double method(double num1, double num2){}
12. 创建一个Employee类, 属性有(名字,年龄,职位,薪水),提供3个构造性别,:方法,!可以初始化(1)(名字,性别,年龄,1职位,薪水),(2)(名字,性别,年龄)(3)(职位,薪水),要求充分复用构造器
class Employee{
String name;
String gender;
int age;
String position;
double salary;
public Employee(String position, double salary){
this.position = position;
this.salary = salary;
}
public Employee(String name, String gender, int age){
this.name = name;
this.gender = gender;
this.age = age;
}
public Employee(String name, String gender, int age, String position, double salary){
this(name,gender,age);
this.position = position;
this.salary = salary;
}
}
13.将对象作为参数传递给方法。
public class homework13 {
public static void main(String[] args){
Circle c = new Circle();
PassObject p = new PassObject();
p.printAreas(c,5);
}
}
class Circle{
double radius;
final float pi=3.14f;//定义常量pi(圆周率)
public double findArea(){
double area = pi*radius*radius;
return area;
}
}
class PassObject{
public void printAreas(Circle c, int times){
for (int i = 1; i <= times; i++){
c.radius = i;
double area = c.findArea();
System.out.println("半径:"+i);
System.out.println("面积:"+area);
}
}
}