欢迎光临
我们一直在努力

面向对象编程(基础部分)作业

1.编写类A01,定义方法max,实现求某个double数组的最大值,并返回Homework01.java

public class Homework01{
public static void main(String[] args) {
double[] arr = {10.2,20.4,50.8,-12.2,36.5};
A01 m = new A01();
System.out.println("该数组的最大值是:" + m.max(arr));
}
}

class A01{

public Double max(double[] arr){
if(arr != null && arr.length > 0){
double max = arr[0];
for(int i = 1;i<arr.length;i++){
if(max < arr[i]){
max = arr[i];
}
}
return max;
}else{
return null;
}
}

}

2.编写类A02,定义方法find,实现查找某字符串是否在字符串数组中,并返回索引,如果找不到,返回-1.Homework02.java

public class Homework02{
public static void main(String[] args) {
String [] strs = {"amy","zy","yjn","jack"};
A02 a02 = new A02();
int index = a02.find("yjn",strs);
System.out.println("查找的index=" + index);
}
}
class A02{
//形参是查找的字符串,字符串数组
public int find(String findStr,String[] strs){
//遍历字符串数组,如果找到,返回索引
for(int i = 0;i < strs.length;i++){
if(findStr.equals(strs[i])){
return i;
}
}
return -1;
}
}

3.编写类Book,定义方法updatePrice,实现更改某本书的价格,具体:如果价格>150,则更改为150,如果价格>100,更改为100,否则不变 Homework03.java

import java.util.Scanner;

public class Homework03 {
public static void main(String[] args) {
Book book = new Book();
System.out.println("价格变更后为:" + book.updatePrice(40.2));
}
}

class Book {
double price;
public Double updatePrice(double price) {
if(price > 150){
price = 150;
return price;
}else if(price > 100){
price = 100;
return price;
}else if(price <= 0){
System.out.println("您输入的价格应该大于0");
return -1.0;
}else {
return price;
}
}
}

4.编写类A03,实现数组的复制功能copyArr,输入旧数组,返回一个新数组,元素和旧数组一样 Homework04.java

public class Homework04 {
public static void main(String[] args) {
int[] oldArr = {10,20,40,50,70};
A03 a03 = new A03();
int[] newArr = a03.CopyArr(oldArr);
System.out.println("新数组的元素为:");
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;
}
}

5.定义一个圆类Circle,定义属性:半径,提供显示圆周长功能的方法,提供显示圆面积的方法 Homework05.java

public class Homework05 {
public static void main(String[] args) {
Circle circle = new Circle(4);
System.out.println("圆的周长是:" + circle.len());
System.out.println("圆的面积是:" + circle.area());

}
}

class Circle {
//定义半径
double radius;
//新建一个构造器用于接收半径
public Circle(double radius){
this.radius = radius;
}
//新建一个成员方法,圆周长
public double len(){
return 2 * Math.PI * radius;
}
//新建一个成员方法,圆面积
public double area(){
return Math.PI * radius * radius;
}
}

6.编程创建一个Cale计算类,在其中定义2个变量表示两个操作数,定义四个方法实现求和、差、乘、商(要求除数为0的话,要提示)并创建两个对象,分别测试HomeworkO6.java

public class Homework06 {
public static void main(String[] args) {
Cale cale = new Cale(45,6);
System.out.println("和为:" + cale.he());
System.out.println("差为:" + cale.cha());
System.out.println("乘为:" + cale.cheng());
Double divRes = cale.shang();
if(divRes != null) {
System.out.println("商为:" + divRes);
}
}
}
class Cale {
double num1;
double num2;
public Cale(double num1,double num2){
this.num1 = num1;
this.num2 = num2;
}
public double he(){
return num1 + num2;
}
public double cha(){
return num1 – num2;
}
public double cheng(){
return num1 * num2;
}
public Double shang(){
if(num2 != 0){
return num1 / num2;
}else{
System.out.println("除数不能为0");
return null;
}

}

}

7.设计一个Dog类,有名字、颜色和年龄属性,定义输出方法show()显示其信息。并创建对象,进行测试、[提示 this.属性]Homework07.java

public class Homework07 {
public static void main(String[] args) {
Dog dog = new Dog();
dog.show("小黄","白色",3);
}
}

class Dog {
String name;
String color;
int age;
public void show(String name,String color,int age){
this.name = name;
this.color = color;
this.age = age;
System.out.println("小狗的名字是:" + this.name + ",颜色是:" + this.color + ",年龄是:" +this.age);
}
}

9.定义Music类,里面有音乐名name、音乐时长times属性,并有播放play功能和近回本身属性信息的功能方法getlnfo.Homework09.java

public class Homework09 {
public static void main(String[] args) {
Music music = new Music("第九交响曲",200);
music.play();
System.out.println(music.getInfo());
}
}

class Music {
String name;
int times;
boolean play;
public Music(String name,int times){
this.name = name;
this.times = times;
}
public void play(){
System.out.println("音乐" +name + "正在播放中,时长" + times + "秒");
}
public String getInfo(){

return "音乐名是:" +name + ",音乐时长为:" + times + "秒";
}
}

11.在测试方法中,调用method方法,代码如下,编译正确,试写出method方法的定义形式。Homework11.

public class Homework11 {
public static void main(String[] args) {
Method m = new Method();
double result = m.method(m.method(10.0,20.0),100);
System.out.println(result);
}
}

class Method{
public double method(double d1,double d2){
return d1 + d2;
}
public double method(double d1,int d2){
return d1 * d2;
}
}

12.创建一个Employee类,属性有(名字,性别,年龄,职位,薪水),提供3个构造方法,可以初始化(1)(名字,性别,年龄,职位,薪水),(2)(名字,性别,年龄)(3)(职位,薪水),要求充分复用构造器 Homework12.java

public class Homework12 {
public static void main(String[] args) {
Employee employee1 = new Employee("zy",'女',18,"工程师",20000.0);
Employee employee2 = new Employee("yjn",'女',20);
Employee employee3 = new Employee("老师",5000.0);
employee1.showInfo();
employee2.showInfo();
employee3.showInfo();
}
}

class Employee{
String name;
char gender;
int age;
String work;
double sal;

public Employee(String name,char gender,int age) {
this.name = name;
this.gender = gender;
this.age = age;
}
public Employee(String work,double sal) {
this.work = work;
this.sal = sal;
}
public Employee(String name,char gender,int age,String work,double sal) {
this(name,gender,age);
this.work = work;
this.sal = sal;
}
public void showInfo(){
System.out.println("姓名:" + this.name + ",性别:" + this.gender + ",年龄:"
+ this.age + ",职位:" + this.work + ",薪水:" + this.sal);
}
}

13.将对象作为参数传递给方法。Homework13.java

public class Homework13 {
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++){
c.setRadius(i);//修改c的半径值
System.out.println((double)i + "\\t" + c.findArea());

}
}
}

赞(0)
未经允许不得转载:171主机测评 » 面向对象编程(基础部分)作业
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址