前言:
本次发布的题目集1-3,主要是对类和对象的使用的考察,题量比较适中,难度设置合理,如风扇类设计比较简单,整体就是为了熟悉类的使用,后续的题目加入了一些要求,加强对类和对象的熟悉,难点集中在每次的答题系统迭代上,非常考验类之间的关联使用。每次题目集都会有一些简单的基础题目来熟悉一下操作,然后就是十分需要思考的迭代题目。
分析与设计:
题目集一:
设计一个风扇Fan类
题目很简单,就是设计一个类根据输入的数据输出相应的信息,也没有难点
代码如下:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int speed = scanner.nextInt();
boolean on = scanner.nextBoolean();
double radius = scanner.nextDouble();
String color = scanner.next();
Fan fan1= new Fan();
Fan fan2 = new Fan(speed,on,radius,color);
System.out.println(fan2);
}
}
class Fan{
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
public boolean isOn() {
return on;
}
public void setOn(boolean on) {
this.on = on;
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
private int speed;
private boolean on;
private double radius;
private String color;
public Fan() {
speed = 1;
on = false;
radius = 5;
color = "white";
}
public Fan(int fanSpeed,boolean fanOn,double fanRadius,String fanColor) {
this.speed=fanSpeed;
this.on=fanOn;
this.radius=fanRadius;
this.color=fanColor;
}
public String toString() {
if (!isOn()){
return "-------\n" +
"Default\n" +
"-------\n" +
"speed 1\n" +
"color white\n" +
"radius 5.0\n" +
"fan is off\n" +
"-------\n" +
"My Fan\n" +
"-------\n"+
"speed " +getSpeed()+
"\ncolor " +getColor()+
"\nradius " +getRadius()+
"\nfan is off";
}
else {
return "-------\n" +
"Default\n" +
"-------\n" +
"speed 1\n" +
"color white\n" +
"radius 5.0\n" +
"fan is off\n" +
"-------\n" +
"My Fan\n" +
"-------\n"+
"speed " +getSpeed()+
"\ncolor " +getColor()+
"\nradius " +getRadius()+
"\nfan is on";
}
}
}
类和对象的使用
也是很简单,就是要将信息存在对象中然后调用方法输出
代码如下:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String name= scanner.next();
String sex =scanner.next();
int age = scanner.nextInt();
String major =scanner.next();
String id = scanner.next();
Student student = new Student(name,sex,id,age,major);
student.printInfo();
}
}
class Student{
private String stName;
private String stSex;
private String stStID;
private int stAge;
private String stMajor;
public Student() {
}
public Student(String name,String sex,String studentID,int age,String major){
this.stName=name;
this.stSex=sex;
this.stStID=studentID;
this.stAge= age;
this.stMajor=major;
}
public String getStName() {
return stName;
}
public void setStName(String stName) {
this.stName = stName;
}
public String getStSex() {
return stSex;
}
public void setStSex(String stSex) {
this.stSex = stSex;
}
public String getStStID() {
return stStID;
}
public void setStStID(String stStID) {
this.stStID = stStID;
}
public int getStAge() {
return stAge;
}
public void setStAge(int stAge) {
this.stAge = stAge;
}
public String getStMajor() {
return stMajor;
}
public void setStMajor(String stMajor) {
this.stMajor = stMajor;
}
public String toString(){
return "姓名:" +getStName() + ",性别:"+ getStSex()+",学号:"+getStStID()+",年龄:"+getStAge()+",专业:"+getStMajor();
}
public void printInfo(){
System.out.println(toString());
}
}
总结:
通过本次题目集,了解到了类和对象的基本使用方法,以及一些实用的工具类的使用
标签:总结,题目,String,void,第一次,radius,return,speed,public From: https://www.cnblogs.com/stznsln/p/18149595