1.编写学生类和成绩类并测试
学生有姓名()和成绩(sGrade)信息,成绩有课程(sGrade)和分数(score)信息。学生类的showResult方法用于输出学生姓名、课程和成绩信息。编写学生类(Student)和成绩类(Grade)并测试。
思路:成绩和学生是学生有成绩的关系,即一个类中有另一个类的成分 ,所以用组合的方式来实现
class Grade{
private String course;
private String score;
public String getCourse() {
return course;
}
public void setCourse(String course) {
this.course = course;
}
public String getScore() {
return score;
}
public void setScore(String score) {
this.score = score;
}
}
class Student{
private String sName;
private Grade sGrade;
public Student(String sName,Grade sGrade){
this.sName = sName;
this.sGrade = sGrade;
}
public void showResult(){
System.out.print("学生的姓名:"+sName);
System.out.print(",课程:"+sGrade.getCourse());
System.out.println(",成绩为:"+sGrade.getScore());
}
}
public class Practice1 {
public static void main(String[] args){
Grade sGrade = new Grade();
sGrade.setCourse("math");
sGrade.setScore("100");
Student student = new Student("小明",sGrade);
student.showResult();
}
}
2.设计和实现一个接口
该接口能播放声音,调节声音的大小,接口的功能将由收音机(Radio)和手机(Mobilephone)两种设备来实现,最后设计一个应用程序类来使用这些实现接口的声音设备。设计要求为当程序运行时先询问用户想使用哪个设备,然后程序按照该设备的工作方式来输出声音。
interface Soundable{
void displaySound();
void turnDown();
void turnUp();
}
class Radio implements Soundable{
@Override
public void displaySound() {
System.out.println("收音机播放广播");
}
@Override
public void turnDown() {
System.out.println("降低收音机音量");
}
@Override
public void turnUp() {
System.out.println("调高收音机音量");
}
}
class Mobilephone implements Soundable{
@Override
public void displaySound() {
System.out.println("手机发出来电铃声");
}
@Override
public void turnDown() {
System.out.println("手机发出来电铃声");
}
@Override
public void turnUp() {
System.out.println("调高手机音量");
}
}
class SampleDisplay{
public void display(Soundable soundable){
soundable.displaySound();
soundable.turnDown();
soundable.turnUp();
}
}
public class Practice2 {
public static void main(String[] args){
Scanner scanner =new Scanner(System.in);
System.out.println("你想使用什么收听?请输入:");
System.out.println("1-收音机 2-手机");
int choice;
SampleDisplay sampleDisplay= new SampleDisplay();
choice = scanner.nextInt();
switch (choice){
case 1:
sampleDisplay.display(new Radio());
break;
case 2:
sampleDisplay.display(new Mobilephone());
break;
}
}
}
3.登录程序的接口设计与实现
package com.chapter5;
import java.util.Scanner;
class User {
private String userName;
private String passWord;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassWord() {
return passWord;
}
public void setPassWord(String passWord) {
this.passWord = passWord;
}
}
interface Loginable {
void loginNull();
void loginFail();
void loginSuccess();
}
class UserVerify implements Loginable {
@Override
public void loginNull() {
System.out.println("用户名或密码不能为空");
}
@Override
public void loginFail() {
System.out.println("用户名或密码错误");
}
@Override
public void loginSuccess() {
System.out.println("登录成功");
}
public int verify(User user) {
String userName = user.getUserName();
String passWord = user.getPassWord();
if (userName.isEmpty() || passWord.isEmpty()) {
return 0;
}
if (userName.equals("admin") || passWord.equals("123456")) {
return 1;
}
return -1;
}
}
public class Practice3 {
public static void main(String[] args) {
String name, passWord;
Scanner scanner = new Scanner(System.in);
System.out.println("请输入用户名");
name = scanner.nextLine();
System.out.println("请输入密码");
passWord = scanner.nextLine();
User user = new User();
user.setUserName(name);
user.setPassWord(passWord);
UserVerify userVerify = new UserVerify();
int code;
code = userVerify.verify(user);
switch (code) {
case 0:
userVerify.loginNull();break;
case 1:
userVerify.loginSuccess();break;
case -1:
userVerify.loginFail();break;
}
}
}
|