实验二 面向对象(一)(实验报告)
实验二 面向对象(一)(实验报告)
姓名:
XXX
班级:
XXX
学号:
XXX
一、实验目的
在集成开发环境下,运用Java语言的面向对象的特性来实现类的封装、构造方法的定义以及对象的实例化和对象方法的调用。
二、实验目标
1. 能够根据需要解决的实际问题,利用Java语言实现类的定义;
2. 能够熟练运用构造方法来实例化对象以及调用对象的方法。
三、实验内容
3.1 实验环境
IntelliJ IDEA Ultimate Edition 2021.2 x64. + openjdk-17.0.1.
3.2 具体实验内容
问题一
- 定义一个计算机类,要求该类中至少包含3个私有的属性(为每个属性定义设置和获取方法),该类中至少包含两个构造方法(一个无参,一个有参),以及包含2-4个方法来描述计算机对象的行为(其中一个方法要能打印输出计算机对象的所有属性值)。
问题分析:定义类用
class
关键字,一个计算机的属性有很多,列举几个即可,使用alt + insert
可以快速为每个属性增加设置和获取方法
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.SQLOutput;
import java.util.Scanner;
public class QuestionOne {
public static void main(String[] args) throws IOException {
Computer computer =
new Computer("DESKTOP-HJCF1OD",
"Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz 2.59 GHz",
"32.0 GB (31.9 GB usable)",
"C659EABC-1D0D-4F6A-9958-E300C6E81453",
"00330-80000-00000-AA007",
"64-bit operating system, x64-based processor");
computer.showParameter();
computer.computer();
computer.openSoftWare();
}
}
class Computer {
private String deviceName;
private String processor;
private String installedRAM;
private String deviceID;
private String productId;
private String systemType;
public Computer(String deviceName, String processor, String installedRAM, String deviceID, String productId, String systemType) {
this.deviceName = deviceName;
this.processor = processor;
this.installedRAM = installedRAM;
this.deviceID = deviceID;
this.productId = productId;
this.systemType = systemType;
}
public String getProductId() {
return productId;
}
public void setProductId(String productId) {
this.productId = productId;
}
public Computer() { }
public String getDeviceName() {
return deviceName;
}
public void setDeviceName(String deviceName) {
this.deviceName = deviceName;
}
public String getProcessor() {
return processor;
}
public void setProcessor(String processor) {
this.processor = processor;
}
public String getInstalledRAM() {
return installedRAM;
}
public void setInstalledRAM(String installedRAM) {
this.installedRAM = installedRAM;
}
public String getDeviceID() {
return deviceID;
}
public void setDeviceID(String deviceID) {
this.deviceID = deviceID;
}
public String getSystemType() {
return systemType;
}
public void setSystemType(String systemType) {
this.systemType = systemType;
}
@Override
public String toString() {
return "deviceName='" + deviceName + '\n' +
"processor='" + processor + '\n' +
"installedRAM='" + installedRAM + '\n' +
"deviceID='" + deviceID + '\n' +
"productId='" + productId + '\n' +
"systemType='" + systemType + '\n';
}
public void showParameter() {
System.out.println(this.toString());
}
public void openSoftWare() throws IOException {
System.out.println("请输入要打开的软件(路径):");
Scanner scanner = new Scanner(System.in);
String cmd = scanner.next();
Process process = Runtime.getRuntime().exec(cmd);
}
public void computer() {
double a = 0, b = 0;
System.out.println("请输入两个数(空格分隔):");
Scanner scanner = new Scanner(System.in);
a = scanner.nextDouble();
b = scanner.nextDouble();
System.out.println("请选择操作符:");
String operation = scanner.next();
switch (operation) {
case "+" -> System.out.println(a + b);
case "-" -> System.out.println(a - b);
case "*" -> System.out.println(a * b);
case "/" -> {
if (b == 0) {
System.out.println("除数为0");
break;
}
System.out.println(a / b);
}
default -> System.out.println("计算机没有该功能");
}
}
}
- 实验结果
deviceName=’DESKTOP-HJCF1OD
processor=’Intel® Core™ i7-9750H CPU @ 2.60GHz 2.59 GHz
installedRAM=’32.0 GB (31.9 GB usable)
deviceID=’C659EABC-1D0D-4F6A-9958-E300C6E81453
productId=’00330-80000-00000-AA007
systemType=’64-bit operating system, x64-based processor请输入两个数(空格分隔):
2 6
请选择操作符:
+
8.0
请输入要打开的软件(路径):
notepad
问题二
- 定义一个类,该类中包含以下几个方法(静态):
1.实现字符串数组的逆序,输出结果为字符串数组;
2.求两个整形数组的交集;
3.求两个浮点型数组的并集;
问题分析:
1.逆序的方式有很多,我使用开辟一个新空间,把原来的字符串反着拷贝到新的空间里
public static String[] stringDecSort (String[] a) {
String[] c = new String[a.length];
for (int i = a.length - 1, j = 0; i >= 0; i--, j++)
c[i] = a[j];
return c;
}
问题分析:
2.两个集合的交集是两个集合共有的元素,使用双重循环,从第一个数组取一个元素,依次与第二个数组的每个元素比较,如果一样就插入到新的数组里,不一样就什么都不错,当第一个数组遍历完了,新得到的数组就是交集的数组
public static int[] intersection (int[] a, int[] b) {
ArrayList<Integer> arrayList = new ArrayList<>();
for (int j : b)
for (int k : a)
if (j == k)
arrayList.add(j);
int[] c = new int[arrayList.size()];
for (int i = 0; i < arrayList.size(); i++)
c[i] = arrayList.get(i);
return c;
}
问题分析:
3.求并集的方法是先把一个数组放到新的数组里,然后依次取第二个数组的元素跟新的数组里的元素比较,如果相同就不插入,不相同就插入
public static double[] unionSet (double[] a, double[] b) {
ArrayList<Double> arrayList = new ArrayList<>();
for (var i : a)
arrayList.add(i);
for (var j : b) {
boolean isTheSame = false;
for (Double aDouble : arrayList) {
if (j == aDouble) {
isTheSame = true;
break;
}
}
if (!isTheSame) arrayList.add(new Double(j));
}
double[] c = new double[arrayList.size()];
for (int i = 0; i < arrayList.size(); i++)
c[i] = arrayList.get(i);
return c;
}
整个的代码
import java.io.IOException;
import java.sql.SQLOutput;
import java.util.*;
public class QuestionTwo {
public static void main(String[] args) {
String[] a = {"zz", "ac", "os", "ii"};
System.out.println(Arrays.toString(Operation.stringDecSort(a)));
int[] arr1 = {1,2,3,4,5,6};
int[] arr2 = {2,3,4};
System.out.println(Arrays.toString(Operation.intersection(arr1, arr2)));
double[] arr3 = {1,2,3,4,5,6};
double[] arr4 = {2,3,4,10};
System.out.println(Arrays.toString(Operation.unionSet(arr3, arr4)));
}
}
class Operation {
public static String[] stringDecSort (String[] a) {
String[] c = new String[a.length];
for (int i = a.length - 1, j = 0; i >= 0; i--, j++)
c[i] = a[j];
return c;
}
public static int[] intersection (int[] a, int[] b) {
ArrayList<Integer> arrayList = new ArrayList<>();
for (int j : b)
for (int k : a)
if (j == k)
arrayList.add(j);
int[] c = new int[arrayList.size()];
for (int i = 0; i < arrayList.size(); i++)
c[i] = arrayList.get(i);
return c;
}
public static double[] unionSet (double[] a, double[] b) {
ArrayList<Double> arrayList = new ArrayList<>();
for (var i : a)
arrayList.add(i);
for (var j : b) {
boolean isTheSame = false;
for (Double aDouble : arrayList) {
if (j == aDouble) {
isTheSame = true;
break;
}
}
if (!isTheSame) arrayList.add(new Double(j));
}
double[] c = new double[arrayList.size()];
for (int i = 0; i < arrayList.size(); i++)
c[i] = arrayList.get(i);
return c;
}
}
- 实验结果
[ii, os, ac, zz]
[2, 3, 4]
[1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 10.0]
- 错误分析
使用foreach循环语句不能将零时元素进行修改
问题三
- 定义一个表示学生信息的类Student,要求如下:
属性名 | 含义 |
---|---|
sNO | 表示学号 |
sName | 表示姓名 |
sSex | 表示性别 |
sJava | 表示Java课程成绩 |
1.类Student带参数的构造方法:
在构造方法中通过形参完成对成员变量的赋值操作。
2.类Student的方法成员:
成员函数名 | 含义 |
---|---|
getNo() | 获得姓名 |
getSex() | 获得性别 |
getAge() | 获得年龄 |
getJava() | 获得Java 课程成绩 |
根据类Student的定义,创建五个该类的对象,输出每个学生的信息,计算并输出这五个学生Java语言成绩的平均值,以及计算并输出他们Java语言成绩的最大值和最小值。
import java.util.Arrays;
public class QuestionThree {
public static void main(String[] args) {
var s = new Student[]{
new Student("01", "a", "男", 19, 98.5),
new Student("02", "b", "女", 19, 68.5),
new Student("03", "b", "男", 21, 90),
new Student("04", "d", "男", 20, 100),
new Student("05", "e", "女", 20, 60)
};
double java = 0, min = s[0].sJava(), max = 0;
for (var i : s) {
java += i.sJava();
if (min > i.sJava())
min = i.sJava();
if (max < i.sJava())
max = i.sJava();
}
java /= s.length;
System.out.println(Arrays.toString(s));
System.out.println("平均值:" + java + "最小值:" + min + "最大值:" + max);
}
}
record Student(String sNo, String sName, String sSex, int sAge, double sJava) { }
- 实验结果
[Student[sNo=01, sName=a, sSex=男, sAge=19, sJava=98.5], Student[sNo=02, sName=b, sSex=女, sAge=19, sJava=68.5], Student[sNo=03, sName=b, sSex=男, sAge=21, sJava=90.0], Student[sNo=04, sName=d, sSex=男, sAge=20, sJava=100.0], Student[sNo=05, sName=e, sSex=女, sAge=20, sJava=60.0]]
平均值:83.4最小值:60.0最大值:100.0
- 错误分析
未简化的类
class Student {
private final String sNo;
private final String sName;
private final String sSex;
private final int sAge;
private final double sJava;
public Student(String sNo, String sName, String sSex, int sAge, double sJava) {
this.sNo = sNo;
this.sName = sName;
this.sSex = sSex;
this.sAge = sAge;
this.sJava = sJava;
}
public String getsNo() {
return sNo;
}
public String getsName() {
return sName;
}
public String getsSex() {
return sSex;
}
public int getsAge() {
return sAge;
}
public double getsJava() {
return sJava;
}
}
四、实验总结
学习和使用了java类,掌握了定义类属性、类方法,可以理解面向对象编程,会使用面向对象编程的方法解决问题,能说出静态方法和普通方法的区别,熟悉访问限定符的修饰作用,本次实验学到了很多
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/122841.html