面向对象练习
1、创建School(学校)类,具有名称和地址属性及输出这些信息的功能
public class School { String name;//学校名称 String location;//地址 public String display(){ return "学校名称:"+name+",地址:"+location; } } class SchoolTest{ public static void main(String[] args) { School school=new School(); school.name="清华大学"; school.location="北京"; System.out.println(school.display()); } }
2、创建学员类和教员类,需求见PPT
//学员类 public class Student { String name; int age; String grade; String hobby; public String display() { return name + "\n" + "年龄:" + age + "\n" + "就读于:" + grade + "\n" + "爱好:" + hobby; } }
//教员类 public class Teacher { String name; String specialty; String course; int age; public String display() { return name + "\n" + "专业方向:" + specialty + "\n" + "教授课程:" + course + "\n" + "教龄:" + age; } }
//测试类 public class Test { public static void main(String[] args) { Student student = new Student(); student.name = "张浩"; student.age = 10; student.grade = "S1班"; student.hobby = "篮球"; System.out.println(student.display()); System.out.println(); Teacher teacher = new Teacher(); teacher.name = "王老师"; teacher.age = 5; teacher.specialty = "计算机"; teacher.course = "使用Java语言理解程序逻辑"; System.out.println(teacher.display()); } }
3、输出门票价格,需求见PPT
import java.util.Scanner; public class Visitor { String name; int age; public void tickets(int age, String name) { if (age >= 12 && age <= 100) { System.out.println(name + "的年龄为:" + age + ",门票价格为:20元"); } else if (age >= 0 && age < 12) { System.out.println(name + "的年龄为:" + age + ",门票免费"); } } } class VisitorTest { public static void main(String[] args) { Visitor v = new Visitor(); Scanner sc = new Scanner(System.in); while (true) { System.out.print("请输入姓名:"); String name = sc.next(); if (name.equals("n")) { System.out.println("退出"); break; } else { System.out.print("请输入年龄:"); int age = sc.nextInt(); v.tickets(age, name); } System.out.println(); } } }
4、用户密码管理,需求见PPT
import java.util.Scanner; public class Longin { String username1 = "admin1"; String password1 = "111111"; public void Login1(String username, String password) { if (username.equals(username1) && password.equals(password1)) { System.out.println(); Scanner sc = new Scanner(System.in); System.out.print("请输入新密码:"); String password1 = sc.next(); System.out.println("修改密码成功,您的新密码为:" + password1); } else { System.out.println("用户名和密码不匹配!您没有权限更新管理员信息。"); } } } class LoginTest { public static void main(String[] args) { Longin lg = new Longin(); Scanner sc = new Scanner(System.in); System.out.print("请输入用户名:"); String username = sc.next(); System.out.print("请输入密码:"); String password = sc.next(); lg.Login1(username, password); } }
5、实现简易计算器,通过键盘输入两个整数并完成两个整数[int]的加减乘除四则运算
import java.util.Scanner; public class JiSuan { public int sum(int a, int b) { return a + b; } public int difference(int a, int b) { return a - b; } public int product(int a, int b) { return a * b; } public double quotient(double a, double b) { return a / b; } } class JiSuanTest { public static void main(String[] args) { JiSuan js = new JiSuan(); Scanner sc = new Scanner(System.in); System.out.print("请输入第一个整数"); int a = sc.nextInt(); System.out.print("请输入第二个整数"); int b = sc.nextInt(); System.out.println("和:" + js.sum(a, b)); System.out.println("差:" + js.difference(a, b)); System.out.println("积:" + js.product(a, b)); if (b == 0) { System.out.println("0不能做除数"); } else { System.out.println("商:" + js.quotient(a, b)); } } }