Java2EE基础练习及面试题_chapter04面向对象(上_02)
- 题目21
- 题目22
- 题目23
- 题目24
- 题目25
- 题目26
- 题目27
- 题目28
- 题目29
- 题目30
- 题目31
- 题目32
- 题目33
- 题目34
- 题目35
- 题目36
- 题目37
- 题目38
- 题目39
- 题目40
- 题目41
申明: 未经许可,禁止以任何形式转载,若要引用,请标注链接地址
全文共计13845字,阅读大概需要10分钟
欢迎关注我的个人公众号:不懂开发的程序猿
题目21
/*
类的实例化
代码实现
编写一个Student类,包含name、gender、age、id、score属性,分别为String、String、int、int、double类型。
类中声明一个say方法,返回String类型,方法返回信息中包含所有属性值。
在另一个StudentTest类中的main方法中,创建Student对象,并访问say方法和所有属性,并将调用结果打印输出。
*/package com.jerry.exer;
/**
* @author jerry_jy
* @create 2022-09-27 8:36
*/
public class Exer1 {
/*
类的实例化
代码实现
编写一个Student类,包含name、gender、age、id、score属性,分别为String、String、int、int、double类型。
类中声明一个say方法,返回String类型,方法返回信息中包含所有属性值。
在另一个StudentTest类中的main方法中,创建Student对象,并访问say方法和所有属性,并将调用结果打印输出。
*/
public static void main(String[] args) {
Student student = new Student("jerry", "man", 20, 1001, 99.9);
student.say();
}
}
class Student {
String name;
String gender;
int age;
int id;
double score;
public Student() {
}
public Student(String name, String gender, int age, int id, double score) {
= name;
this.gender = gender;
this.age = age;
this.id = id;
this.score = score;
}
public String say() {
String str = "我的名字是:" + name + ",性别是:" + gender + ",年龄是:" + age + ",学号是:" + id + ",分数是:" + score;
System.out.println(str);
return str;
}
}题目22
/*
定义一个丈夫Husband类,有姓名、年龄、妻子属性
定义一个妻子Wife类,有姓名、年龄、丈夫属性
丈夫类中有一个getInfo方法,其中,能显示自己的姓名,年龄,和他的妻子的姓名,年龄
妻子类中有一个getInfo方法,其中,能显示自己的姓名,年龄,和她的丈夫的姓名,年龄
定义一个测试类,创建妻子和丈夫对象,然后测试
*/package com.jerry.exer;
/**
* @author jerry_jy
* @create 2022-09-27 16:47
*/
public class Exer2 {
/*
定义一个丈夫Husband类,有姓名、年龄、妻子属性
定义一个妻子Wife类,有姓名、年龄、丈夫属性
丈夫类中有一个getInfo方法,其中,能显示自己的姓名,年龄,和他的妻子的姓名,年龄
妻子类中有一个getInfo方法,其中,能显示自己的姓名,年龄,和她的丈夫的姓名,年龄
定义一个测试类,创建妻子和丈夫对象,然后测试
*/
public static void main(String[] args) {
Husband husband = new Husband("tom",22);
Wife wife = new Wife("jerry", 20);
husband.getInfo(wife);
wife.getInfo(husband);
}
}
class Husband{
String name;
int age;
Wife wife;
public Husband() {
}
public Husband(String name, int age) {
= name;
this.age = age;
}
public void getInfo(Wife wife){
System.out.println("我的名字是:"+name+",年龄是:"+age+",妻子的姓名是:"++",妻子的年龄是:"+wife.age);
}
}
class Wife{
String name;
int age;
Husband husband;
public Wife() {
}
public Wife(String name, int age) {
= name;
this.age = age;
}
public void getInfo(Husband husband){
System.out.println("我的名字是:"+name+",年龄是:"+age+",丈夫的姓名是:"++",丈夫的年龄是:"+husband.age);
}
}题目23
/*
定义银行账户类Account,有属性:卡号cid,余额balance,所属用户Customer
银行账户类Account有方法:
(1)getInfo(),返回String类型,返回卡的详细信息
(2)取钱方法withdraw(),参数自行设计,如果取钱成功返回true,失败返回false
(3)存钱方法save(),参数自行设计,如果存钱成功返回true,失败返回false
其中Customer类有姓名、身份证号、联系电话、家庭地址等属性
Customer类有方法say(),返回String类型,返回他的个人信息。
在测试类Bank中创建银行账户类对象和用户类对象,并设置信息,与显示信息
*/package com.jerry.exer;
/**
* @author jerry_jy
* @create 2022-09-27 17:08
*/
public class Exer3 {
/*
定义银行账户类Account,有属性:卡号cid,余额balance,所属用户Customer
银行账户类Account有方法:
(1)getInfo(),返回String类型,返回卡的详细信息
(2)取钱方法withdraw(),参数自行设计,如果取钱成功返回true,失败返回false
(3)存钱方法save(),参数自行设计,如果存钱成功返回true,失败返回false
其中Customer类有姓名、身份证号、联系电话、家庭地址等属性
Customer类有方法say(),返回String类型,返回他的个人信息。
在测试类Bank中创建银行账户类对象和用户类对象,并设置信息,与显示信息
*/
public static void main(String[] args) {
Customer customer = new Customer("jerry", "123", "456", "789");
customer.say();
Account account = new Account(1001, 10000.0,customer);
account.getInfo();
account.save(1000.0);
account.withdraw(20000);
}
}
class Account {
private int cid;
private double balance;
private Customer customer;
public Account() {
}
public Account(int cid, double balance) {
this.cid = cid;
this.balance = balance;
}
public Account(int cid, double balance, Customer customer) {
this.cid = cid;
this.balance = balance;
this.customer = customer;
}
public int getCid() {
return cid;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public String getInfo() {
String str = "卡号cid:" + cid + ",余额balance:" + balance + ",所属用户Customer:" + customer;
System.out.println(str);
return str;
}
public boolean withdraw(double amount) {//取钱
if (amount > balance) {
System.out.println("余额不足,取款失败!");
return false;
}
System.out.println("取款成功!本次取款金额为:" + amount);
this.setBalance(this.getBalance() - amount);
return true;
}
/**
* 存款
*
* @param amount
* @return
*/
public boolean save(double amount) {
System.out.println("存款成功!本次存款金额为:" + amount);
this.setBalance(this.getBalance() + amount);
return true;
}
}
class Customer {
private String name;
private String IDCard;
private String phone;
private String address;
public Customer() {
}
public Customer(String name, String IDCard, String phone, String address) {
= name;
this.IDCard = IDCard;
this.phone = phone;
this.address = address;
}
public void say(){
System.out.println("我的名字是:"+name+",身份证是:"+IDCard+",联系电话是:"+phone+",家庭地址是:"+address);
}
}题目24
/*
方法的声明与调用
(1)声明一个圆柱体类型,
(2)声明属性:底边的半径,和高
(3)声明方法:
A:方法的功能:在方法中打印圆柱体的详细信息
圆柱体的底边的半径是xxx,高是xxx,底面积是xxx,体积是xxx。
B:方法的功能:返回底面积
C:方法的功能:返回体积
D:方法的功能:为圆柱体的底边的半径,和高赋值
E:方法的功能:为圆柱体的底边的半径,和高赋值,并返回赋值的结果
如果底边的半径或高为<=0,赋值失败,返回false,否则返回true
(4)并测试
*/package com.jerry.exer;
/**
* @author jerry_jy
* @create 2022-09-27 17:28
*/
public class Exer4 {
/*
方法的声明与调用
(1)声明一个圆柱体类型,
(2)声明属性:底边的半径,和高
(3)声明方法:
A:方法的功能:在方法中打印圆柱体的详细信息
圆柱体的底边的半径是xxx,高是xxx,底面积是xxx,体积是xxx。
B:方法的功能:返回底面积
C:方法的功能:返回体积
D:方法的功能:为圆柱体的底边的半径,和高赋值
E:方法的功能:为圆柱体的底边的半径,和高赋值,并返回赋值的结果
如果底边的半径或高为<=0,赋值失败,返回false,否则返回true
(4)并测试
*/
public static void main(String[] args) {
Cylinder cylinder = new Cylinder();
cylinder.setParam(3.0, 5.0);
cylinder.printDetails();
System.out.println("底面积:"+cylinder.getArea());
System.out.println("体积:"+cylinder.getVolume());
boolean b = cylinder.setValue(3.0, -1);
if (b){
System.out.println("赋值失败");
}else {
cylinder.printDetails();
}
}
}
class Cylinder {
double radius;
double height;
public Cylinder() {
}
public Cylinder(double radius, double height) {
this.radius = radius;
this.height = height;
}
public void printDetails() {
double area = Math.PI * radius * radius;
double volume = area * height;
System.out.println("圆柱体的底边的半径是" + radius + ",高是" + height + ",底面积是" + area + ",体积是" + volume);
}
public double getArea() {
double area = Math.PI * radius * radius;
return area;
}
public double getVolume() {
double volume = getArea() * height;
return volume;
}
public void setParam(double radius,double height){
this.radius=radius;
this.height=height;
}
public boolean setValue(double radius,double height){
this.radius=radius;
this.height=height;
if (radius<=0||height<=0){
return false;
}
return true;
}
}题目25
/*
编程创建一个Box类,在其中定义三个变量表示一个立方体的长、宽和高,定义一个方法求立方体的体积。创建一个对象,求给定尺寸的立方体的体积。(提供无参的构造器和一个有参的构造器)
*/public static void main(String[] args) {
Box box = new Box(3.0, 4.0, 5.0);
double volume = box.getVolume();
System.out.println("给定尺寸的立方体的体积: " + volume);
}
}
class Box {
double length, width, height;
public Box() {
}
public Box(double length, double width, double height) {
this.length = length;
this.width = width;
this.height = height;
}
public double getVolume() {
double volume = length * width * height;
return volume;
}题目26
/*
定义一个圆类型 提供显示圆周长功能的方法 提供显示圆面积的方法 提供无参的构造器和一个有参的构造器
*/package com.jerry.exer;
/**
* @author jerry_jy
* @create 2022-09-27 19:01
*/
public class Exer6 {
/*
定义一个圆类型
提供显示圆周长功能的方法
提供显示圆面积的方法
提供无参的构造器和一个有参的构造器
*/
public static void main(String[] args) {
Circle circle = new Circle(3.0);
System.out.println("⚪的周长:" + circle.getCircle());
System.out.println("⚪的面积:" + circle.getArea());
}
}
class Circle {
double radius;
public Circle() {
}
public Circle(double radius) {
this.radius = radius;
}
public double getCircle() {
double circle = Math.PI * radius * 2;
return circle;
}
public double getArea() {
double area = Math.PI * radius * radius;
return area;
}
}题目27
/*设计一个Dog类,有名字、颜色和年龄属性,定义构造器初始化这些属性,定义输出方法show()显示其信息。
*/package com.jerry.exer;
/**
* @author jerry_jy
* @create 2022-09-27 19:05
*/
public class Exer7 {
/*
设计一个Dog类,有名字、颜色和年龄属性,定义构造器初始化这些属性,定义输出方法show()显示其信息。
*/
public static void main(String[] args) {
Dog dog = new Dog("jerry", "white", 3);
dog.show();
}
}
class Dog {
String name;
String color;
int age;
public Dog() {
}
public Dog(String name, String color, int age) {
= name;
this.color = color;
this.age = age;
}
public void show() {
System.out.println("狗的名字:" + name + ",颜色是:" + color + ",年龄是:" + age);
}
}题目28
/*
定义一个类,用于描述坐标点
0——————>X
|
|
| P(X,Y)
|
|
Y(1)具有计算当前点到原点距离的功能
(2)求到任意一点(m,n)的距离
(3)求到任意一点(Point p)的距离
(4)具有坐标点显示功能,显示格式(x,y)
(5)提供无参的构造器和一个有参的构造器
*/package com.jerry.exer;
/**
* @author jerry_jy
* @create 2022-09-27 19:08
*/
public class Exer8 {
/*
定义一个类,用于描述坐标点
0——————>X
|
|
| P(X,Y)
|
|
Y
(1)具有计算当前点到原点距离的功能
(2)求到任意一点(m,n)的距离
(3)求到任意一点(Point p)的距离
(4)具有坐标点显示功能,显示格式(x,y)
(5)提供无参的构造器和一个有参的构造器
*/
public static void main(String[] args) {
Aix aix = new Aix(3.0, 4.0);
System.out.println("当前坐标点:" + aix.showPoint());
System.out.println("计算当前点到原点距离: " + aix.getDistanceToO());
System.out.println("计算到任意一点(m,n)的距离: " + aix.getDistanceToMN(5.0, 6.0));
System.out.println("计算到任意一点(Point p)的距离: " + aix.getDistanceToPoint(new Aix(1.0, 2.0)));
}
}
class Aix {
double x, y;
public Aix() {
}
public Aix(double x, double y) {
this.x = x;
this.y = y;
}
/**
* @return 具有计算当前点到原点距离的功能
*/
public double getDistanceToO() {
double distance = Math.sqrt(x * x + y * y);
return distance;
}
/**
* @param m
* @param n
* @return 求到任意一点(m,n)的距离
*/
public double getDistanceToMN(double m, double n) {
double distance = Math.sqrt((x - m) * (x - m) + (y - n) * (y - n));
return distance;
}
/**
* @param aix
* @return 求到任意一点(Point p)的距离
*/
public double getDistanceToPoint(Aix aix) {
double distance = Math.sqrt((x - aix.x) * (x - aix.x) + (y - aix.y) * (y - aix.y));
return distance;
}
public String showPoint() {
String str = "(" + x + ", " + y + ")";
return str;
}
}题目29
/*写一个人的类 属性:名字,性别,年龄;提供无参的构造器和一个有参的构造器 方法:(1)自我介绍的方法(2)吃饭的方法 创建一个对象“张三”
*/package com.jerry.exer;
/**
* @author jerry_jy
* @create 2022-09-27 19:25
*/
public class Exer9 {
/*
写一个人的类
属性:名字,性别,年龄;提供无参的构造器和一个有参的构造器
方法:(1)自我介绍的方法(2)吃饭的方法
创建一个对象“张三”
*/
public static void main(String[] args) {
Person person = new Person("张三", "男", 20);
person.introduce();
person.eat();
}
}
class Person {
String name;
String gender;
int age;
public Person() {
}
public Person(String name, String gender, int age) {
= name;
this.gender = gender;
this.age = age;
}
public void introduce() {
System.out.println("我叫:" + name + ",性别:" + gender + ",年龄:" + age);
}
public void eat() {
System.out.println("吃饭中.......");
}
}题目30
/*写一个汽车类:属性:品牌;车长;颜色;价格;创建五个对象:“捷达”,“宝马”,“劳斯莱斯”,“科鲁兹”,“迈锐宝” 提供无参的构造器和一个有参的构造器*/package com.jerry.exer;
/**
* @author jerry_jy
* @create 2022-09-27 19:29
*/
public class Exer10 {
/*
写一个汽车类:
属性:品牌;车长;颜色;价格;
创建五个对象:“捷达”,“宝马”,“劳斯莱斯”,“科鲁兹”,“迈锐宝”
提供无参的构造器和一个有参的构造器
*/
public static void main(String[] args) {
Car car1 = new Car("捷达", 5.2, "白", 350000);
System.out.println(car1.toString());
Car car2 = new Car("宝马", 5.3, "黑", 360000);
System.out.println(car2.toString());
Car car3 = new Car("劳斯莱斯", 5.4, "棕", 370000);
System.out.println(car3.toString());
Car car4 = new Car("科鲁兹", 5.5, "蓝", 380000);
System.out.println(car4.toString());
Car car5 = new Car("迈锐宝", 5.6, "灰", 390000);
System.out.println(car5.toString());
}
}
class Car {
String brand;
double length;
String color;
double price;
public Car() {
}
public Car(String brand, double length, String color, double price) {
this.brand = brand;
this.length = length;
this.color = color;
this.price = price;
}
@Override
public String toString() {
return "Car{" +
"brand='" + brand + '\'' +
", length=" + length +
", color='" + color + '\'' +
", price=" + price +
'}';
}
}题目31
/*写一个课程类:属性:课程名;学时;任课老师;创建五个对象:“c语言”,“java编程”,“php网络编程”,“c++”,“数据结构”提供无参的构造器和一个有参的构造器
*/package com.jerry.exer;
/**
* @author jerry_jy
* @create 2022-09-27 19:34
*/
public class Exer11 {
/*
写一个课程类:
属性:课程名;学时;任课老师;
创建五个对象:“c语言”,“java编程”,“php网络编程”,“c++”,“数据结构”
提供无参的构造器和一个有参的构造器
*/
public static void main(String[] args) {
Lecture lecture1 = new Lecture("c语言", "48H", "Zhang3");
System.out.println(lecture1.toString());
Lecture lecture2 = new Lecture("java编程", "47H", "Li4");
System.out.println(lecture2.toString());
Lecture lecture3 = new Lecture("php网络编程", "46H", "Wang5");
System.out.println(lecture3.toString());
Lecture lecture4 = new Lecture("c++", "45H", "Zhao6");
System.out.println(lecture4.toString());
Lecture lecture5 = new Lecture("数据结构", "44H", "Qian8");
System.out.println(lecture5.toString());
}
}
class Lecture{
String className, classHour, classTeacher;
public Lecture() {
}
public Lecture(String className, String classHour, String classTeacher) {
this.className = className;
this.classHour = classHour;
this.classTeacher = classTeacher;
}
@Override
public String toString() {
return "Lecture{" +
"className='" + className + '\'' +
", classHour='" + classHour + '\'' +
", classTeacher='" + classTeacher + '\'' +
'}';
}
}题目32
/*关于参数传递 练习一 */public static void leftshift(int i, int j) {
i += j;
}
public static void main(String args[]) {
int i = 4, j = 2;
leftshift(i, j);
System.out.println(i);//答案是i=4,不要被里面的 leftshift(i, j)函数迷惑,调用这个函数后确实是i=6,但是这个函数没有返回值,因此打印的i还是初始化显示定义的4
}题目33
/*
练习二
写出结果。
*/public static void main(String[] args) {
int[] a = new int[1];
modify(a);
System.out.println(a[0]); //1
}
public static void modify(int[] a) {
a[0]++;
}题目34
//练习三
//写出结果int i;
void change(int i) {
i++;
System.out.println(i);
}
void change1(Exer12 t) {
t.i++;
System.out.println(t.i);
}
public static void main(String[] args) {
Exer12 ta = new Exer12();
System.out.println(ta.i); //0
ta.change(ta.i);//1
System.out.println(ta.i); //0
ta.change1(ta); //1
System.out.println(ta.i);//1
}题目35
/*
练习四
写出结果
*/
class Value {
int i = 15;
}
public static void main(String argv[]) {
Exer12 t = new Exer12();
t.first();
}
public void first() {
int i = 5;
Value v = new Value();
v.i = 25;
second(v, i);
System.out.println(v.i);//15
}
public void second(Value v, int i) {
i = 0;
v.i = 20;
Value val = new Value();
v = val;
System.out.print(v.i + " " + i);//020
}题目36
/*
练习六
import java.util.Arrays;
public class PassValueExer2{
public static void main(String[] args){
int[] array = {3,2,5,1,7};
//调用sort方法,实现从大到小排序
//在此处补充代码
....
//显示结果
System.out.println("排序后的结果是:" + Arrays.toString(array));
}
//要求使用冒泡排序完成
public void sort(//形参?){
}
}
*/题目37
// 以下代码的执行结果是什么public static void main(String[] args) {
int i = 0;
change(i);
i = i++;
System.out.println("i = " + i);//0
}
public static void change(int i) {
i++;
}题目38
// 以下代码的执行结果是什么public static void main(String[] args) {
String str = "world";
char[] ch = new char[]{'h','e','l','l','o'};
change(str,ch);
System.out.println(str);//world
System.out.println(String.valueOf(ch));//abcde
}
public static void change(String str, char[] arr){
str = "change";
arr[0] = 'a';
arr[1] = 'b';
arr[2] = 'c';
arr[3] = 'd';
arr[4] = 'e';
}题目39
// 以下代码的执行结果是什么int a;
int b;
public void f() {
a = 0;
b = 0;
int[] c = {0};
g(b, c);
System.out.println(a + " " + b + " " + c[0]);//1 0 1
}
public void g(int b, int[] c) {
a = 1;
b = 1;
c[0] = 1;
}
public static void main(String[] args) {
Exer12 t = new Exer12();
t.f();
}题目40
/*
简答
当一个对象被当作参数传递到一个方法后,此方法可改变这个对象的属性,并可返回变化后的结果,那么这里到底是值传递还是引用传递?
答:是值传递。Java 编程语言只有值传递参数。当一个对象实例作为一个参数被传递到方法中时,参数的值就是对该对象的引用。对象的内容可以在被调用的方法中改变,但对象的引用是永远不会改变的
*/题目41
// 补充程序class Circle {
private double radius;
public Circle(double r) {
radius = r;
}
public Circle compare(Circle cir) {
// 程序代码
/*
* if(this.radius>cir.radius) return this; return cir;
*/
// return (this.radius>cir.radius)?this: cir;
}
}
class TC {
public static void main(String[] args) {
Circle cir1 = new Circle(1.0);
Circle cir2 = new Circle(2.0);
Circle cir;
cir = cir1.compare(cir2);
if (cir1 == cir)
System.out.println("圆1的半径比较大");
else
System.out.println("圆2的半径比较大");
}
}
















