编程题部分
Posted 3cH0-Nu1L
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了编程题部分相关的知识,希望对你有一定的参考价值。
Circle类:
输入:
4 5
-4 -2
输出:
this is a constructor Circle [radius=2] c1:area=12 this is a constructor Circle [radius=2] Circle [radius=4] c2:area=50 this is a constructor with para Circle [radius=5] c3:area=78
this is a constructor Circle [radius=2] c1:area=12 this is a constructor Circle [radius=2] Circle [radius=2] c2:area=12 this is a constructor with para Circle [radius=2] c3:area=12
代码:
import java.util.Scanner; import java.lang.Math.*; public class Main { public static class Circle { private int radius; Circle() { this.radius = 2; System.out.println("this is a constructor"); } Circle(int radius) { this.radius = radius; if (this.radius <= 0) { this.radius = 2; // System.out.println("this is a constructor with para"); } System.out.println("this is a constructor with para"); } public void setter(int r){ this.radius = r; if(r <= 0){ this.radius = 2; } } public int getter(){ return this.radius; } public int gerArea(){ double s; s = Math.PI * radius * radius; return (int)s; } public String toString(){ String str = "Circle [radius=" + this.radius + "]"; return str; } } public static void main(String[]args){ // c1: Circle c1 = new Circle(); String str1 = c1.toString(); System.out.println(str1); int s1 = c1.gerArea(); System.out.println("c1:area=" + s1); // c2: Circle c2 = new Circle(); String str2 = c2.toString(); System.out.println(str2); Scanner in = new Scanner(System.in); int r = in.nextInt(); c2.setter(r); String str22 = c2.toString(); System.out.println(str22); int s2 = c2.gerArea(); System.out.println("c2:area=" + s2); // c3: int rr = in.nextInt(); Circle c3 = new Circle(rr); String str3 = c3.toString(); System.out.println(str3); int s3 = c3.gerArea(); System.out.println("c3:area=" + s3); } }
设计一个风扇Fan类:
设计一个名为Fan的类表示一个风扇。这个类包括: 1.三个名为SlOW、MEDIUM和FAST,其值为1、2和3常量表示风扇的速度。 2.一个名为speed的int类型私有数据域表示风扇的速度(默认值为SLOW)。 3.一个名为on的boolean类型私有数据域表示风扇是否打开(默认值为false)。 4.一个名为radius的double类型私有数据域表示风扇的半径(默认值为5)。 5.一个名为color的string类型数据域表示风扇的颜色(默认值为white)。 6.这四个成员变量(数据域)的访问器和修改器。 7.一个无参构造方法; 8.一个有参构造方法 public Fan(int fanSpeed,boolean fanOn,double fanRadius,String fanColor) { ... }, 9.一个名为toString()的方法返回描述风扇的字符串。如果风扇是打开的,那么该方法在一个组合的字符串中返回风扇的速度、颜色和半径。如果风扇没有打开,该方法就会返回一个由"fan is off"和风扇颜色以及半径组合成的字符串。 请在自己的草稿本上画出UML图,养成良好的设计习惯。 要求:创建两个Fan对象: 第一个对象利用无参构造方法构造默认风扇对象Fan1; 第二个对象依据输入的风扇属性,调用有参构造方法构造自定义风扇对象Fan2。 通过调用它们的toString方法显示这些对象。
输入:
2 True 6 Red
输出:
------- Default ------- speed 1 color white radius 5.0 fan is off ------- My Fan ------- speed 2 color Red radius 6.0 fan is on
代码:
import java.util.*; public class Main { public static void main(String[]args){ Fan f1 = new Fan(); String s1 = f1.toString(); System.out.println(s1); Scanner in = new Scanner(System.in); int speed = in.nextInt(); boolean on = in.hasNextBoolean(); double radius = in.nextDouble(); String color = in.next(); Fan f2 = new Fan(); f2.setspeed(speed); f2.setColor(color); f2.seton(on); f2.setradius(radius); String s2 = f2.toString(); System.out.println(s2); } public static class Fan{ static int SLOW=1, MEDIUM=2, FAST=3; private int speed = 1; private boolean on = false; private double radius = 5; String color = "white"; Fan(){ } public Fan(int fanSpeed, boolean fanOn, double fanRadius, String fanColor){ speed = fanSpeed; on = fanOn; radius = fanRadius; color = fanColor; } public void setspeed(int fanspeed){ this.speed = fanspeed; } public void setColor(String fancolor){ this.color = fancolor; } public void setradius(double fanradius){ this.radius = fanradius; } public void seton(boolean fanon){ this.on = fanon; } public String toString(){ String s1, s2, s3, s4, s5; if(this.on == false){ s1 = "-------\\nDefaut\\n-------\\n"; s2 = "speed 1\\n"; s3 = "color white\\n"; s4 = "radius 5.0\\n"; s5 = "fan is off\\n"; } else{ s1 = "-------\\nMy Fan\\n-------"; s2 = "speed " + this.speed + "\\n"; s3 = "color " + this.color + "\\n"; s4 = "radius " + this.radius + "\\n"; s5 = "fan is on\\n"; } return s1 + s2 + s3 + s4 + s5; } } }
设计一个BankAccount类:
输入:
700 70 7
输出:
700 630 637
代码:
import java.util.Scanner; public class Main { public static void main(String[]args){ Scanner in = new Scanner(System.in); int a1 = in.nextInt(); int a2 = in.nextInt(); int a3 = in.nextInt(); BankAccount b = new BankAccount(a1); int a = b.getBlance(); System.out.println(a); b.withdraw(a2); b.deposit(a3); } public static class BankAccount{ public int balance; BankAccount(){ this.balance = 0; } BankAccount(int balance){ this.balance = balance; } public int getBlance(){ return this.balance; } public void withdraw(int amount){ this.balance = this.balance - amount; System.out.println(this.balance); } public void deposit(int a){ this.balance += a; System.out.println(this.balance); } } }
学生类设计:
输入:
tom 6
jerry 8
输出:
无参构造方法 name:无名,age:7 name:tom,age:7
无参构造方法 name:无名,age:7 name:jerry,age:8
代码:
import java.util.Scanner; public class Main { public static void main(String[]args){ Student s1 = new Student(); Scanner in = new Scanner(System.in); String name = in.next(); int age = in.nextInt(); s1.setname(name); s1.setage(age); s1.display(); } public static class Student{ private String name; private int age; public void setname(String name){ this.name = name; } public void setage(int age){ this.age = age; if(age <= 6) this.age = 7; } public String getname(){ return this.name; } public int getage(){ return this.age; } Student(){ System.out.println("无参构造方法"); this.name = "无名"; this.age = 7; System.out.println("name:无名,age:7"); } Student(String name, int age){ this.name = name; this.age = age; } public void display(){ System.out.println("name:" + this.name + ",age:" + this.age); } } }
定义类与创建对象:
输入:
输出:
this person is lili,her age is 19 this person is lucy,her age is 20
代码:
public class Main { public static void main(String[]args){ Person p1 = new Person("lili", 19); Person p2 = new Person("lucy", 20); } public static class Person{ public String name; public int age; Person(String name, int age){ this.name = name; this.age = age; System.out.println("this person is " + this.name + ",her age is " + this.age); } } }
继承覆盖综合练习-Person、Student、Employee、Company:
输入:
s zhang 23 false 001 net15 e wang 18 true 3000.51 IBM s zhang 23 false 001 net15 e bo 25 true 5000.51 IBM e bo 25 true 5000.52 IBM e bo 18 true 5000.54 IBM e tan 25 true 5000.56 IBM e tan 25 true 5000.51 IBM s wang 17 false 002 null s wang 17 false 002 null e hua 16 false 1000 null s wang 17 false 002 net16 e hua 16 false 1000 null e hua 18 false 1234 MicroSoft ! continue
输出:
Employee:bo-18-true-IBM-5000.54 Employee:bo-25-true-IBM-5000.51 Employee:bo-25-true-IBM-5000.52 Employee:hua-16-false-null-1000.0 Employee:hua-16-false-null-1000.0 Employee:hua-18-false-MicroSoft-1234.0 Employee:tan-25-true-IBM-5000.56 Employee:tan-25-true-IBM-5000.51 Student:wang-17-false-002-null Student:wang-17-false-002-null Student:wang-17-false-002-net16 Employee:wang-18-true-IBM-3000.51 Student:zhang-23-false-001-net15 Student:zhang-23-false-001-net15 stuList Student:wang-17-false-002-null Student:wang-17-false-002-net16 Student:zhang-23-false-001-net15 empList Employee:bo-18-true-IBM-5000.54 Employee:bo-25-true-IBM-5000.51 Employee:hua-16-false-null-1000.0 Employee:hua-18-false-MicroSoft-1234.0 Employee:tan-25-true-IBM-5000.56 Employee:tan-25-true-IBM-5000.51 Employee:wang-18-true-IBM-3000.51
代码:
import java.text.DecimalFormat; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner se = new Scanner(System.in); ArrayList <Person> 列表=new ArrayList <Person>(); while (true) { String s或e =se.next(); if(s或e.equals("s")) { 列表.add(new Student(se.next(),se.nextInt(),se.nextBoolean(),se.next(),se.next())); }else if(s或e.equals("e")) { 列表.add(new Employee(se.next(),se.nextInt(),se.nextBoolean(),se.nextDouble(),new Company(se.next()))); }else { break; } } Collections.sort(列表, new 排序类()); for(Person 一个人:列表) { if(一个人 instanceof Student) { System.out.println(((Student)一个人).toString()); }else { System.out.println(((Employee)一个人).toString()); } } String 输入exit或其他=se.next(); if(输入exit或其他.equals("exit")) { return; }else { ArrayList <Student> stuList=new ArrayList <Student>(); ArrayList <Employee> empList=new ArrayList <Employee>(); for(Person w:列表) { if(w instanceof Student) { if(!stuList.contains((Student)w)) { stuList.add((Student)w); } } if(w instanceof Employee) { if(!empList.contains((Employee)w)) { empList.add((Employee)w); } } } System.out.println("stuList"); for(Student a:stuList) { System.out.println(a.toString()); } System.out.println("empList"); for(Employee a:empList) { System.out.println(a.toString()); } } } } abstract class Person{ String name;int age; boolean gender; public Person(String name, int age, boolean gender) { super(); if(name.equals("null")) { this.name = null; }else { this.name = name; } this.age = age; this.gender = gender; } @Override public String toString() { return name + "-" + age + "-" + gender; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Person other = (Person) obj; if (age != other.age) return false; if (gender != other.gender) return false; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; return true; } } class Student extends Person{ String stuNo,clazz; public Student(String name, int age, boolean gender, String stuNo, String clazz) { super(name, age, gender); if(stuNo.equals("null")) { this.stuNo = null; }else { this.stuNo = stuNo; } if(clazz.equals("null")) { this.clazz = null; }else { this.clazz = clazz; } } @Override public String toString() { return "Student:" + super.toString()+"-"+ stuNo + "-" + clazz; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (!super.equals(obj)) return false; if (getClass() != obj.getClass()) return false; Student other = (Student) obj; if (clazz == null) { if (other.clazz != null) return false; } else if (!clazz.equals(other.clazz)) return false; if (stuNo == null) { if (other.stuNo != null) return false; } else if (!stuNo.equals(other.stuNo)) return false; return true; } } class Company{ String name; public Company(String name) { if(!name.equals("null")) { this.name = name; }else { this.name = null; } } @Override public String toString() { return name; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Company other = (Company) obj; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; return true; } } class Employee extends Person{ Company company; double salary; public Employee(String name, int age, boolean gender, double salary, Company company) { super(name, age, gender); this.company = company; this.salary = salary; } @Override public String toString() { return "Employee:" +super.toString()+"-"+ company + "-" + salary ; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (!super.equals(obj)) return false; if (getClass() != obj.getClass()) return false; Employee other = (Employee) obj; String newpersalary = new DecimalFormat("#.#").format(other.salary); String newthissalary = new DecimalFormat("#.#").format(this.salary); if (company == null) { if (other.company != null) return false; } else if (!company.equals(other.company)) return false; if (!newpersalary.equals(newthissalary)) return false; return true; } } class 排序类 implements Comparator<Person>{ @Override public int compare(Person o1, Person o2) { if(o1.name.compareTo(o2.name)>0) { return 1; }else if(o1.name.compareTo(o2.name)<0) { return -1; }else { if(o1.age>o2.age) { return 1; }else if(o1.age<o2.age) { return -1; }else { return 0; } } } }
设计一个Shape及其子类Oval:
输入:
8 6
输出:
The area of Oval(a:8.0,b:6.0) is 150.79644480000002
The perimeterof Oval(a:8.0,b:6.0) is 44.42882862370954
代码:
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); double a = in.nextDouble(); double b = in.nextDouble(); Oval oval = new Oval(a, b); System.out.println("The area of " + oval.toString() + " is " + oval.area()); System.out.println("The perimeterof " + oval.toString() + " is " + oval.perimeter()); } } abstract class以上是关于编程题部分的主要内容,如果未能解决你的问题,请参考以下文章