作业
1、编写一个程序,实现从命令行参数输入两个字符串类型的数值, 并计算输出两个数值的和。 [必做题]
package com.neuedu.day07;
import java.util.Scanner;
import org.junit.Test;
import com.neuedu.day07.homework.Postgraduate;
import com.neuedu.day07.homework.Student;
import com.neuedu.day07.homework.Undergraduate;
public class HomeWork {
@Test
public void myTest01() {
Scanner sc = new Scanner(System.in);
String s1 = sc.next();
String s2 = sc.next();
Integer s11 = new Integer(s1);
Integer s22 = new Integer(s2);
int sum = s11 + s22;
System.out.println(sum);
}
2、编写一个程序,实现从命令行参数输入一字符串, 统计该字符串中字符“e”出现的次数。(识点:String中常用的方法) [必做题]
@Test
public void myTest02() {
Scanner sc = new Scanner(System.in);
String s1 = sc.next();
int length = s1.length();
int sum=0;
for (int i = 0;i<length;i++) {
if(s1.charAt(i)=='e')
{
sum++;
}
}
System.out.println("该字符串中字符“e”出现的次数:"+sum);
}
3.按以下要求编写学生管理系统实现多态:
3.1设计一个学生类Student,包含的属性有姓名name和年龄age,有相关数据的构造方法和set/get方法。
3.2由学生类派生出本科生类Undergraduate和研究生类Postgraduate, 本科生类包含的属性有专业specialty等,
研究生类包含的属性有研究方向studydirection等。有相关数据的构造方法和set/get方法
3.3声明测试类:声明Student类的数组(含5个元素),生成五个对象存入数组:
其中二个Student类的对象、二个Postgraduate类的对象、一个ndergraduate类的对象,
*打印输出每个对象的某个成员变量。将一个父类的引用指向一个子类对象,用这个塑型后的对象来调用某个方法实现多态性。
@Test
public void myTest03() {
Student st1=new Student("张三",14);
Student st2=new Student("李四",15);
Student P1=new Postgraduate("王五",16,"计算机");
Student P2=new Postgraduate("赵六",17,"电子");
Student U1=new Undergraduate("钱七",18,"人工智能");
Student[] s=new Student[5];
s[0]=st1;
s[1]=st2;
s[2]=P1;
s[3]=P2;
s[4]=U1;
for(int i=0;i<s.length;i++)
{
s[i].show();
}
}
}
package com.neuedu.day07.homework;
public class Student {
private String name;
private int age;
public Student() {
super();
}
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void show() {
System.out.println("姓名:" + this.name + " 年龄:" + this.age);
}
}
package com.neuedu.day07.homework;
public class Undergraduate extends Student{
private String specialty;
public Undergraduate() {
super();
}
public Undergraduate(String name, int age,String specialty) {
super(name, age);
this.specialty = specialty;
}
public String getSpecialty() {
return specialty;
}
public void setSpecialty(String specialty) {
this.specialty = specialty;
}
@Override
public void show() {
System.out.println("姓名:"+this.getName()+" 年龄:"+this.getAge()+" 专业:"+this.getSpecialty());
}
}
package com.neuedu.day07.homework;
public class Postgraduate extends Student{
private String studydirection;
public Postgraduate() {
super();
}
public Postgraduate(String name, int age,String studydirection) {
super(name, age);
this.studydirection = studydirection;
}
public String getStudydirection() {
return studydirection;
}
public void setStudydirection(String studydirection) {
this.studydirection = studydirection;
}
@Override
public void show() {
System.out.println("姓名:"+this.getName()+" 年龄:"+this.getAge()+" 研究方向:"+this.getStudydirection());
}
}