当前位置: 首页 > news >正文

java期末完整版

import java.util.Scanner; /** * 任务:计算球的体积。 */ // 请在 Begin-End 之间编写代码 public class Educode { public static void main(String[] args) { Scanner input = new Scanner(System.in); /********** Begin **********/ double r = input.nextDouble(); double v = 4.0 / 3.0 * Math.PI * Math.pow(r, 3); System.out.printf("v = %.3f\n", v); /********** End **********/ } } /**第三题***/ import java.util.Scanner; public class Main { public static void main(String[] args) { int max,min,sum=0; Scanner sc = new Scanner(System.in); int N = sc.nextInt(); int[] data = new int[N]; /********** Begin **********/ for(int i=0; i<N; i++) { data[i] = sc.nextInt(); } max = data[0]; min = data[0]; for(int i=0; i<N; i++) { sum = sum + data[i]; if(data[i] > max) { max = data[i]; } if(data[i] < min) { min = data[i]; } } /********** End **********/ System.out.println("sum="+sum); System.out.println("max="+max); System.out.println("min="+min); sc.close(); } } /****第四题类的定义***/ import java.util.Scanner; class Rectangle { int length; int width; public Rectangle(int length, int width) { this.length = length; this.width = width; } public int area() { return length * width; } } public class Cuboid extends Rectangle { int height; /********** Begin **********/ //定义 Cuboid 类的有参构造方法 public Cuboid(int length, int width, int height) { super(length, width); this.height = height; } //定义 Cuboid 类的业务方法:volume() 和 area(),分别用于计算体积和表面积 public int volume() { return length * width * height; } public int area() { return 2 * (length * width + width * height + height * length); } /********** End **********/ public static void main(String[] args) { Scanner sc = new Scanner(System.in); int length = sc.nextInt(); int width = sc.nextInt(); int height = sc.nextInt(); Cuboid cuboid = new Cuboid(length, width,height); System.out.println("volume=" + cuboid.volume()); System.out.println("area=" + cuboid.area()); sc.close(); } } /***第五题集合遍历set**/ import java.util.Iterator; import java.util.HashSet; import java.util.Scanner; import java.util.Set; public class Main { public static void main(String[] args) { Set<String> set = new HashSet<String>(); Scanner sc = new Scanner(System.in); int N = sc.nextInt(); // 循环输入集合元素 for(int i=0; i<N; i++) { set.add(sc.next()); } sc.close(); Iterator<String> it = set.iterator(); // 使用迭代器对象,遍历输出集合元素 while(it.hasNext()) { System.out.println(it.next()); } } } /***第六题:数组求和***/ import java.util.Scanner; public class Main { public static void main(String[] args) { int max,sum=0; Scanner sc = new Scanner(System.in); int N = sc.nextInt(); int[] arr = new int[N]; for(int i=0; i<N; i++) { arr[i] = sc.nextInt(); } max = arr[0]; for(int i=0; i<N; i++) { sum = sum + arr[i]; if(arr[i] > max) { max = arr[i]; } } System.out.println("sum=" + sum); System.out.println("max=" + max); sc.close(); } } /***第七题编写 Teacher 类***/ import java.util.Scanner; class Student { // 私有变量 private int id; private String name; // 无参构造方法 public Student() { this.id = 0; this.name = "未知"; } // 有参构造方法 public Student(int id, String name) { this.id = id; this.name = name; } // 普通方法 introduce() public void introduce() { System.out.printf("我是%s,工号是%d\n", this.name, this.id); } // 封装性: getId()、setId()、getName()、setName() public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } } public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); //用户输入学号、姓名 int id = scanner.nextInt(); String name = scanner.next(); scanner.nextLine(); //用户输入修改后的学号、姓名 int newId = scanner.nextInt(); String newName = scanner.next(); scanner.close(); // 测试无参构造 Student student1 = new Student(); student1.introduce(); // 测试有参构造 Student student2 = new Student(id, name); student2.introduce(); //测试修改属性 student2.setId(newId); student2.setName(newName); student2.introduce(); // 测试获取属性 System.out.println("工号: " + student2.getId()); System.out.println("姓名: " + student2.getName()); } } /***第 8 题:集合遍历 List***/ import java.util.ArrayList; import java.util.Iterator; import java.util.Scanner; public class Main { public static void main(String[] args) { ArrayList<String> list = new ArrayList<String>(); Scanner sc = new Scanner(System.in); int N = sc.nextInt(); // 循环输入集合元素 for(int i=0; i<N; i++) { list.add(sc.next()); } sc.close(); Iterator<String> it = list.iterator(); // 使用迭代器对象,遍历输出集合元素 while(it.hasNext()) { System.out.println(it.next()); } } } /***第 9 题:设计继承父类 Animal 的 Dog 和 Cat 类***/ import java.util.Scanner; // 抽象父类 Animal abstract class Animal { // 保护属性 name,age protected String name; protected int age; // 构造方法 public Animal(String name, int age) { this.name = name; this.age = age; } // 抽象业务方法 voice()、eat() abstract void voice(); abstract void eat(); } // 子类 Dog 继承父类 Animal class Dog extends Animal { // 构造方法 public Dog(String name, int age) { super(name, age); } // 重写业务方法 voice()、eat() @Override void voice() { System.out.println(this.name + "汪汪叫"); } @Override void eat() { System.out.println(this.name + "吃骨头"); } } // 子类 Cat 继承父类 Animal class Cat extends Animal { // 构造方法 public Cat(String name, int age) { super(name, age); } // 重写业务方法 voice()、eat() @Override void voice() { System.out.println(this.name + "喵喵叫"); } @Override void eat() { System.out.println(this.name + "吃鱼"); } } public class TestAnimal { public static void main(String[] args) { Scanner input = new Scanner(System.in); // 输入 Dog 和 Cat 信息 String dogName = input.next(); int dogAge = input.nextInt(); String catName = input.next(); int catAge = input.nextInt(); // 创建 Dog 和 Cat 类对象 Dog dog = new Dog(dogName, dogAge); Cat cat = new Cat(catName, catAge); // Dog 和 Cat 类对象的行为 AnimalAction(dog); System.out.println(dog.name + dog.age + "岁"); AnimalAction(cat); System.out.println(cat.name + cat.age + "岁"); } // 动物行为 AnimalAction public static void AnimalAction(Animal animal) { animal.voice(); animal.eat(); } } /***第 10 题:设计继承父类 Person 的 Chinese 和 English 类***/ import java.util.Scanner; // 抽象类 Person abstract class Person { // 私有属性 name、sex 和 age private String name; private String sex; private int age; // 三个参数的构造方法 public Person(String name, String sex, int age) { this.name = name; this.sex = sex; this.age = age; } // Getter 方法 public String getName() { return name; } public String getSex() { return sex; } public int getAge() { return age; } // 抽象方法 eat() abstract void eat(); } // 子类 Chinese 继承抽象父类 Person class Chinese extends Person { // 三个参数的构造方法 public Chinese(String name, String sex, int age) { super(name, sex, age); } // 重写父类的 eat() 方法 @Override void eat() { System.out.printf("姓名:%s,性别:%s,年龄:%d,我是中国人,我喜欢吃米饭!\n", getName(), getSex(), getAge()); } // 业务方法:练习太极拳 shadowBoxing() public void shadowBoxing() { System.out.printf("%s在练习太极拳!\n", getName()); } } // 子类 English 继承抽象父类 Person class English extends Person { // 三个参数的构造方法 public English(String name, String sex, int age) { super(name, sex, age); } // 重写父类的 eat() 方法 @Override void eat() { System.out.printf("姓名:%s,性别:%s,年龄:%d,我是英国人,我喜欢吃三明治!\n", getName(), getSex(), getAge()); } // 业务方法:练习骑马 horseRiding() public void horseRiding() { System.out.printf("%s在练习骑马!\n", getName()); } } public class Educode { public static void main(String[] args) { Scanner input = new Scanner(System.in); // 输入中国人信息 String name = input.next(); String sex = input.next(); int age = input.nextInt(); // 创建 Chinese 类的对象 Person chinese = new Chinese(name, sex, age); // 输入英国人信息 name = input.next(); sex = input.next(); age = input.nextInt(); // 创建 English 类的对象 Person english = new English(name, sex, age); // 输出中国人的信息 showEat(chinese); showSport(chinese); // 输出英国人的信息 showEat(english); showSport(english); } // 显示喜欢吃的食品 public static void showEat(Person person) { person.eat(); } // 显示在练习的运动(中国人:太极拳;英国人:骑马)。注意:要进行向下转型操作。 public static void showSport(Person person) { if(person instanceof Chinese) { Chinese chinese = (Chinese) person; chinese.shadowBoxing(); } else if(person instanceof English) { English english = (English) person; english.horseRiding(); } } } /***第 11 题:圆锥体的体积***/ import java.util.Scanner; public class Educode { public static void main(String[] args) { Scanner input = new Scanner(System.in); double r = input.nextDouble(); double h = input.nextDouble(); double v = 1.0 / 3.0 * Math.PI * r * r * h; System.out.printf("v = %.4f", v); } } /***第 12 题:银行账户 Account 类**/ import java.util.Scanner; public class Educode { public static void main(String[] args) { // Input Scanner input = new Scanner(System.in); int id = input.nextInt(); String name = input.next(); double balance = input.nextDouble(); double annualInterestRate = input.nextDouble(); // Caculate Account account = new Account(id, name, balance, annualInterestRate); // 取款 2500 account.withdraw(2500); // 存款 3000 account.deposit(3000); // Output account.print(); } } // 银行账户类 Account class Account { // 4 个私有属性 id name balance annualInterestRate private int id; private String name; private double balance; private double annualInterestRate; // 有参构造方法 public Account(int id, String name, double balance, double annualInterestRate) { this.id = id; this.name = name; this.balance = balance; this.annualInterestRate = annualInterestRate; } // Setter & Getter 方法 public int getId() { return id; } public void setId(int id) { this.id = id; } 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 double getAnnualInterestRate() { return annualInterestRate; } public void setAnnualInterestRate(double annualInterestRate) { this.annualInterestRate = annualInterestRate; } // 业务方法 withdraw(double amount),从账户中取特定数额的款 public void withdraw(double amount) { if (amount > balance) { System.out.println("取款失败"); } else { balance -= amount; } } // 业务方法 deposit(double amount),往账户中存特定数额的款。 public void deposit(double amount) { balance += amount; } // 业务方法 getMonthlyInterestRate(),返回月利率 public double getMonthlyInterestRate() { // 正确月利率计算:年利率 / 100 / 12,即 年利率 / 1200 return annualInterestRate / 1200; } // 业务方法 print(),输出账户信息 (账号、姓名、存款金额、月利率) public void print() { System.out.println(id); System.out.println(name); System.out.println(balance); // 【注意】使用 printf 格式化输出,保留5位小数,并使用 %% 转义输出末尾的 % 号 System.out.printf("%.5f%%\n", getMonthlyInterestRate()); } }
http://www.cnnetsun.cn/news/3050245.html

相关文章:

  • 电商卖家定价核算:毛利率在线计算器实操与行业毛利率参考
  • 5个理由选择FreeShip Plus:零成本专业船舶设计完全指南
  • FMT开源飞控开发(八):电源管理与电池SOC估算
  • android compose TimePicker 时间选择器 使用
  • 【claude code实践】基础命令速览:新手每天都会用到的 Claude Code 操作
  • 云服务器部署私有AI大模型实战指南
  • Qt 铁甲阅读器-搜索
  • [智能体-591]:Python的一个强项目是自动化测试,JS/TS+Node也是自动化测试,比较他们的在自动化测试领域的强弱优缺点对比
  • 深度学习优化
  • 从文件资源管理器到3D预览:STL缩略图扩展的技术突破与应用价值
  • 7个技巧让全面战争MOD开发效率飙升:RPFM现代化工具链深度指南
  • Sublime Text 3 —— 打造沉浸式编码体验:Material主题与Fira Code字体的黄金组合
  • 开源漏洞修复脚本的5个关键执行细节与风险管控实践
  • 普通人也能做专业量化!香港大学免费开源 Vibe-Trading用自然语言来写策略
  • 终极指南:如何用猫抓浏览器扩展一键下载网页视频和音频资源
  • 3分钟掌握图像转字节数组:让OLED开发变得简单的终极免费指南
  • 微信小程序webview实战:从PDF预览到网页内嵌的完整方案与避坑指南
  • SRC漏洞挖掘实战指南:从Web安全基础到高效渗透测试
  • Icarus Verilog深度解析:开源硬件验证工具的技术架构与实战指南
  • OpCore-Simplify:三分钟快速配置黑苹果OpenCore EFI的终极自动化工具
  • 鼠标性能测试神器:MouseTester如何帮你解锁精准输入体验
  • 【招聘】创业公司如何建立猎头合作体系——不被坑的完整指南
  • 【计算机毕业设计案例】基于 SpringBoot+Vue 的体育场地资源统筹预约系统设计 自助式体育馆场地预约服务平台的设计与开发(程序+文档+讲解+定制)
  • 基于MSP430FR6047的超声波水表软件架构解析与开发实践
  • OpCore-Simplify:黑苹果配置的终极简化工具,15分钟完成专业级EFI搭建
  • 如何在Windows、macOS和Linux上免费畅玩Switch游戏:Ryujinx模拟器完整指南
  • LitCAD:免费开源的C二维CAD绘图软件完全指南
  • 缠论智能分析插件ChanlunX:3分钟从零到实战的完整指南
  • Java计算机毕设之基于 Web 技术的在线问卷调查与投票系统的设计与实现 基于 SpringBoot+Vue3 的可视化投票系统(完整前后端代码+说明文档+LW,调试定制等)
  • OpCore-Simplify:30分钟搞定黑苹果配置,告别复杂手动调试的终极解决方案