目录
作者的话
作业一
作业二
大底包含了JAVA硬性规定的作业代码,并非最优解,仅供参考并会持续更新。勿要无脑copy,对自己负责。如果代码有误或者优化建议,直接相应博客下方评论或者qq找我如果对代码有理解不了的或者疑惑可以询问我,但是请确保你已经自己思考过或者查过搜索引擎(如果我原模原样搜到了资料的话我会锤你的hh)。一些语法和库的资料查询网站受个人风格影响,部分题目解题方式可能没按照教学要求,如有必要请自己按教学要求做一遍。如果可以的话,麻烦借鉴完以后给我博客来个三连啥的,这可能对我以后找工作或者其他博客的推广什么的有些帮助,感谢
1. (程序题)
定义一个表示学生信息的类Student,要求如下:
(1)类Student的成员变量:
sNO:表示学号(String);sName:表示姓名(String);sJava:表示Java课程成绩(int)。
(2)类Student带参数的构造方法:
在构造方法中通过形参完成对成员变量的赋值操作。
(3)类Student的方法成员:
getSNo():获得学号; setSNo(....):设置学号;
getSName():获得姓名; setSName(....):设置姓名;
getSJava():获得Java 课程成绩;setSJava(....):设置java课程的成绩;
实现下面方法,从传入的学生数组中返回java成绩最高分
public static int getHigh(Student[] students) { }(4)在mian方法中,根据类Student的定义,创建学生对象数组,并进行对象成员变量的赋值,计算并输出学生对象数组java成绩的最高分。
首先输入学生的个数,然后依次输入每个学生的学号、姓名、成绩
输出:这些学生的最高分。
例如:
4
101 zs 90
102 ls 80
103 ww 98
104 lk 92输出:
98
import java.util.Scanner;
public class Main {
public static class Student {
private String sNO;
private String sName;
private int sJava;
public Student(String sNO, String sName,int sJava) {
this.sNO = sNO;
this.sName = sName;
this.sJava = sJava;
}
public String getsNO() {
return sNO;
}
public void setsNO(String sNO) {
this.sNO = sNO;
}
public String getsName() {
return sName;
}
public void setsName(String sName) {
this.sName = sName;
}
public int getsJava() {
return sJava;
}
public void setsJava(int sJava) {
this.sJava = sJava;
}
}
public static int getHigh(Student[] students) {
int maxScore = 0;
for (int i = 0; i < students.length; i++) {
if (students[i].getsJava() > maxScore) {
maxScore = students[i].getsJava();
}
}
return maxScore;
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n = input.nextInt();
Student []students = new Student[n];
for (int i = 0; i < n; i++) {
String sNO = input.next();
String sName = input.next();
int sJava = input.nextInt();
students[i] = new Student(sNO, sName, sJava);
}
int maxScore = getHigh(students);
System.out.println(maxScore);
}
}
基于C++的代码
#include
#include
using namespace std;
class Student {
private:
string sNO;
string sName;
int sJava;
public:
Student(string sNO, string sName, int sJava) {
this->sNO = sNO;
this->sName = sName;
this->sJava = sJava;
}
Student() {
sNO = "";
sName = "";
sJava = 0;
}
string getsNO() {
return sNO;
}
void setsNO(string sNO) {
this->sNO = sNO;
}
string getsName() {
return sName;
}
void setsName(string sName) {
this->sName = sName;
}
int getsJava() {
return sJava;
}
void setsJava(int sJava) {
this->sJava = sJava;
}
};
int getHigh(Student students[], int n) {
int maxScore = 0;
for (int i = 0; i < n; i++) {
if (students[i].getsJava() > maxScore) {
maxScore = students[i].getsJava();
}
}
return maxScore;
}
int main() {
int n;
cin >> n;
Student students[n];
for (int i = 0; i < n; i++) {
string sNO, sName;
int sJava;
cin >> sNO >> sName >> sJava;
students[i] = Student(sNO, sName, sJava);
}
int maxScore = getHigh(students, n);
cout << maxScore << endl;
return 0;
}
基于python的代码
class Student:
def __init__(self, sNO, sName, sJava):
self.sNO = sNO
self.sName = sName
self.sJava = sJava
def getsNO(self):
return self.sNO
def setsNO(self, sNO):
self.sNO = sNO
def getsName(self):
return self.sName
def setsName(self, sName):
self.sName = sName
def getsJava(self):
return self.sJava
def setsJava(self, sJava):
self.sJava = sJava
def getHigh(students):
maxScore = 0
for i in range(len(students)):
if students[i].getsJava() > maxScore:
maxScore = students[i].getsJava()
return maxScore
n = int(input())
students = []
for i in range(n):
sNO, sName, sJava = input().split()
sJava = int(sJava)
student = Student(sNO, sName, sJava)
students.append(student)
maxScore = getHigh(students)
print(maxScore)
定义并实现一个复数类Complex,要求实现复数的加法和减法。
类拥有两个成员变量a和b,分别代表复数的实部和虚部。
类有默认构造方法(a和b的默认值为0),也有拷贝构造方法(参数为复数类对象的引用)。
另外还应该有一个Print方法打印复数,打印格式为a bi,若实部虚部都为0,则输出0。两个复数的实部、虚部在main方法中由键盘输入。
输入:
3 4 1 2(输入两个复数的实部和虚部)
输出:
4 6i(输出两个复数的和的实部和虚部)
2 2i(输出两个复数的差的实部和虚部)
注:输入和输出括号里面的是说明。
import java.util.Scanner;
public class Main {
public static class Complex {
private int a; // 实部
private int b; // 虚部
// 默认构造方法
public Complex() {
this.a = 0;
this.b = 0;
}
// 带参构造方法
public Complex(int a, int b) {
this.a = a;
this.b = b;
}
// 拷贝构造方法
public Complex(Complex c) {
this.a = c.a;
this.b = c.b;
}
// 复数加法
public Complex add(Complex c) {
int new_a = this.a + c.a;
int new_b = this.b + c.b;
return new Complex(new_a, new_b);
}
// 复数减法
public Complex subtract(Complex c) {
int new_a = this.a - c.a;
int new_b = this.b - c.b;
return new Complex(new_a, new_b);
}
// 打印复数
public void print() {
if (this.a == 0 && this.b == 0) {
System.out.println("0");
} else if (this.a == 0) {
System.out.println(this.b + "i");
} else if (this.b == 0) {
System.out.println(this.a);
} else {
System.out.println(this.a + " " + this.b + "i");
}
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int a1 = scanner.nextInt();
int b1 = scanner.nextInt();
int a2 = scanner.nextInt();
int b2 = scanner.nextInt();
Complex c1 = new Complex(a1, b1);
Complex c2 = new Complex(a2, b2);
Complex c3 = c1.add(c2);
Complex c4 = c1.subtract(c2);
c3.print();
c4.print();
}
}
基于C++的代码
#include
using namespace std;
class Complex {
private:
double a; // 实部
double b; // 虚部
public:
// 默认构造方法
Complex() {
this->a = 0;
this->b = 0;
}
// 带参构造方法
Complex(double a, double b) {
this->a = a;
this->b = b;
}
// 拷贝构造方法
Complex(const Complex& c) {
this->a = c.a;
this->b = c.b;
}
// 复数加法
Complex add(Complex c) {
double new_a = this->a + c.a;
double new_b = this->b + c.b;
return Complex(new_a, new_b);
}
// 复数减法
Complex subtract(Complex c) {
double new_a = this->a - c.a;
double new_b = this->b - c.b;
return Complex(new_a, new_b);
}
// 打印复数
void print() {
if (this->a == 0 && this->b == 0) {
cout << "0" << endl;
} else if (this->a == 0) {
cout << this->b << "i" << endl;
} else if (this->b == 0) {
cout << this->a << endl;
} else {
cout << this->a << "+" << this->b << "i" << endl;
}
}
};
int main() {
double a1, b1, a2, b2;
cin >> a1 >> b1 >> a2 >> b2;
Complex c1(a1, b1);
Complex c2(a2, b2);
Complex c3 = c1.add(c2);
Complex c4 = c1.subtract(c2);
c3.print();
c4.print();
return 0;
}
基于python的代码
class Complex:
def __init__(self, a=0, b=0):
self.a = a
self.b = b
def add(self, c):
new_a = self.a + c.a
new_b = self.b + c.b
return Complex(new_a, new_b)
def subtract(self, c):
new_a = self.a - c.a
new_b = self.b - c.b
return Complex(new_a, new_b)
def print(self):
if self.a == 0 and self.b == 0:
print("0")
elif self.a == 0:
print(str(self.b) + "i")
elif self.b == 0:
print(str(self.a))
else:
print(str(self.a) + "+" + str(self.b) + "i")
a1, b1, a2, b2 = map(float, input().split())
c1 = Complex(a1, b1)
c2 = Complex(a2, b2)
c3 = c1.add(c2)
c4 = c1.subtract(c2)
c3.print()
c4.print()
作业三
设计用户实体类User,包含用户姓名,账户余额,地址;全属性构造函数
设计商品实体类Product,包含商品名称,商品价格,全属性构造函数
*由于同一商品可购买多个,设计购买项实体类Item,包含购买商品,商品的购买数量,全属性构造函数
*设计订单实体类Order,包含本次订单的用户,购买的全部商品项,本次购买总花费(后续商品价格的改变不应对本次已购买数据产生影响),全属性构造函数
实体类应包含属性对应的按命名约定编写的getter/setter方法设计订单业务逻辑处理类OrderService,提供静态方法addOrder(),实现:
传入购买的用户,以及本次购买的全部购买项
当用户余额大于购买商品总花费时,创建订单,封装购买用户/购买项/总花费,并返回订单对象
当余额小于总花费时,返回空引用
此业务方法的返回类型?参数?实现具体业务逻辑创建测试类及主函数,假设已有四类商品及单价分别如下:
Product p1 = new Product("方便面", 5.5);
Product p2 = new Product("火腿肠", 2.2);
Product p3 = new Product("矿泉水", 1.0);
Product p4 = new Product("饮料", 3.5);
输入用户姓名,账户余额,地址模拟用户,输入购买商品的种类数,再输入购买的商品名称及数量,
(提示此处根据商品名称,得到对应的商品对象。字符串的比较用String类的equals()方法)
调用订单逻辑方法测试。
当订单创建成功时,打印商品总额,用户余额,地址;当订单创建失败时,打印用户余额不足
结果保留2位小数输入:
张三 100 nefu
2
方便面 2
矿泉水 1
输出:
购买商品总额:12.00
用户余额:100.00
地址:nefu
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// 创建商品
Product p1 = new Product("方便面", 5.5);
Product p2 = new Product("火腿肠", 2.2);
Product p3 = new Product("矿泉水", 1.0);
Product p4 = new Product("饮料", 3.5);
// 创建用户
Scanner scanner = new Scanner(System.in);
//System.out.println("请输入用户名、余额和地址:");
String name = scanner.next();
double balance = scanner.nextDouble();
String address = scanner.next();
User user = new User(name, balance, address);
// 创建订单
//System.out.println("请输入商品数量:");
int count = scanner.nextInt();
Item[] items = new Item[count];
double totalCost = 0;
for (int i = 0; i < count; i++) {
//System.out.println("请输入第" + (i+1) + "件商品的名称和数量:");
String productName = scanner.next();
int quantity = scanner.nextInt();
Product product = null;
switch (productName) {
case "方便面":
product = p1;
break;
case "火腿肠":
product = p2;
break;
case "矿泉水":
product = p3;
break;
case "饮料":
product = p4;
break;
default:
System.out.println("没有该商品!");
i--;
continue;
}
Item item = new Item(product, quantity);
items[i] = item;
totalCost += product.getPrice() * quantity;
}
Order order = new Order(user, items, totalCost);
// 处理订单
if (user.getBalance() < order.getCost()) {
System.out.println("用户余额不足");
} else {
//user.setBalance(user.getBalance() - order.getCost());
System.out.println("购买商品总额:" + String.format("%.2f", order.getCost()));
System.out.println("用户余额:" + String.format("%.2f", user.getBalance()));
System.out.println("地址:" + user.getAddress());
}
}
}
// 购买项实体类
class Item {
private Product product; // 商品对象
private int quantity; // 商品数量
public Item(Product product, int quantity) {
this.product = product;
this.quantity = quantity;
}
public Product getProduct() {
return product;
}
public void setProduct(Product product) {
this.product = product;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
}
// 订单实体类
class Order {
private User user; // 用户对象
private Item[] items; // 购买的商品项数组
private double cost; // 本次购买总花费
public Order(User user, Item[] items, double cost) {
this.user = user;
this.items = items;
this.cost = cost;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public Item[] getItems() {
return items;
}
public void setItems(Item[] items) {
this.items = items;
}
public double getCost() {
return cost;
}
public void setCost(double cost) {
this.cost = cost;
}
}
// 商品实体类
class Product {
private String name; // 商品名称
private double price; // 商品价格
public Product(String name, double price) {
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
// 用户实体类
class User {
private String name; // 用户姓名
private double balance; // 用户账户余额
private String address; // 用户地址
public User(String name, double balance, String address) {
this.name = name;
this.balance = balance;
this.address = address;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
// 订单业务逻辑处理类
class OrderService {
private Order[] orders; // 订单数组
private int numOfOrders; // 订单数量
private static Scanner scanner = new Scanner(System.in); // 用于输入的 Scanner 对象
// 构造方法
public OrderService(int capacity) {
orders = new Order[capacity];
numOfOrders = 0;
}
// 添加订单
public void addOrder(User user, Item[] items) {
double cost = 0;
for (Item item : items) {
cost += item.getProduct().getPrice() * item.getQuantity();
}
if (user.getBalance() < cost) {
System.out.println("余额不足,下单失败!");
return;
}
Order order = new Order(user, items, cost);
orders[numOfOrders++] = order;
user.setBalance(user.getBalance() - cost);
System.out.println("下单成功!");
}
// 取消订单
public void cancelOrder(int orderId) {
if (orderId >= numOfOrders || orderId < 0) {
System.out.println("不存在该订单,取消失败!");
return;
}
Order order = orders[orderId];
order.getUser().setBalance(order.getUser().getBalance() + order.getCost());
for (Item item : order.getItems()) {
item.getProduct().setPrice(item.getProduct().getPrice() * (1 - item.getQuantity() / 10.0));
}
System.out.println("取消订单成功!");
}
// 显示所有订单
public void showAllOrders() {
System.out.println("所有订单如下:");
for (int i = 0; i < numOfOrders; i++) {
System.out.println("订单号:" + i);
Order order = orders[i];
System.out.println("用户:" + order.getUser().getName());
System.out.println("收货地址:" + order.getUser().getAddress());
System.out.println("订单详情:");
for (Item item : order.getItems()) {
System.out.println(item.getProduct().getName() + " x " + item.getQuantity());
}
System.out.println("总价:" + order.getCost());
System.out.println("---------------------------");
}
}
// 显示某个用户的所有订单
public void showOrdersByUser(User user) {
System.out.println("用户 " + user.getName() + " 的订单如下:");
for (int i = 0; i < numOfOrders; i++) {
Order order = orders[i];
if (order.getUser() == user) {
System.out.println("订单号:" + i);
System.out.println("收货地址:" + order.getUser().getAddress());
System.out.println("订单详情:");
for (Item item : order.getItems()) {
System.out.println(item.getProduct().getName() + " x " + item.getQuantity());
}
System.out.println("总价:" + order.getCost());
System.out.println("---------------------------");
}
}
}
}
这是一个用Java编写的购物系统模拟程序。它创建了四个产品,允许用户创建一个用户对象,并基于用户的输入创建一个订单对象。订单对象包含一个条目数组,这些条目包含有关产品及其数量的信息。
该程序允许用户将商品添加到订单中,检查用户的余额是否足以支付订单,并在用户余额充足时显示总费用、用户余额和地址。
代码还包含了几个类:Product(产品)、User(用户)、Item(订单中的商品)、Order(订单)和OrderService(订单服务)。
Product类表示一个产品,包含有关其名称和价格的信息。
User类表示一个用户,包含有关用户姓名、余额和地址的信息。
Item类表示订单中的商品,包含有关产品及其数量的信息。
Order类表示一个订单,包含有关用户、订单中的商品以及订单的总费用的信息。
OrderService类包含将订单添加到订单数组中并从数组中检索订单的方法。
总的来说,该代码提供了Java中面向对象编程原则的简单示例。