【牛客带你学编程Java方向】项目练习第3期(截止3.13)


Java项目练习:第3期
练习时间:2月27日-3月13日(2周,除去春节)
活动规则:
  • 每一期一个项目,届时会开新帖发布
  • 学员直接将答案提交到该贴评论区即可
  • 两周后,公布导师参考答案
  • 导师评选出当期最佳代码将被设置为精彩回复

奖励:牛客大礼包一份(牛客定制水杯 牛客定制笔 牛客定制程序员徽章 滑稽抱枕)
参与方式:直接将你的代码回复到本帖评论区

-----------------------------------------------------
    
本期题目:

类和对象(20分钟)

需求描述:

设计一个学生类Student和它的子类Undergradute,要求如下:
  1. Student类有name(姓名)和age(年龄),一个包含两个参数的构造方法,用于给name和age属性赋值,一个show()方法打印Student的属性信息。
  2. 本科生类Undergraduate增加一个degree(学位)属性。有一个包含三个参数的构造方法,前两个参数用于给继承的name和age赋值,第三个参数给degree专业赋值,一个show()方法打印Undergrate的属性信息。
  3. 在测试类中创建Student对象和Undergraduate的属性信息。

考察知识点:

  • 面向对象基础
  • 类和对象之间的组合关系。

参考知识点:《java基础入门》第3章、第4章

参与方式:直接将你的代码回复到本帖评论区

全部评论
注:以下三个类代码通过了阿里巴巴Java规范插件验证。 1、Student类 /**  * 牛客网第三题——学生类  */ package com.finersoft.nowcoder; /**  * 名称:学生类 说明:  *   * @author Finersoft  * @date 2018年3月1日  */ public class Student {     /** 定义最长寿命为200岁 */     public final int MAXAGE= 200;     /** * 姓名 */     private String name;     /** * 年龄 */     private int age;     /** * @return the name */     public String getName() {         return name;     }     /** * @param name * the name to set to a student */     public void setName(String name) {         if (name != null && name != "") {             this.name = name;         } else {             System.out.println("姓名不能为空");         }     }     /** * @return the age */     public int getAge() {         return age;     }     /** * @param age the age to set to a student */     public void setAge(int age) {         if (age >= 0 && age <= MAXAGE) {             this.age = age;         } else {             System.out.println("请输入一个正常现代人的年龄");         }     }     /**      * 默认构造函数      */     public Student() {         this("张三", 18);     }     /**      * 带一参数的构造函数      *       * @param name      *            学生姓名      */     public Student(String name) {         this(name, 18);     }     /**      * 完整参数构造函数      *       * @param name      *            学生姓名      * @param age      *            学生年龄      */     public Student(String name, int age) {         this.name = name;         this.age = age;     }     /**      * 打印学生信息      */     public void show() {         System.out.println(this.toString());     }     /**      * 转换成字符串操作      */     @Override     public String toString() {         return "学生信息:\r\n\t姓名:" + this.getName ()+ "\r\n\t年龄:" + this.getAge();     }          public static void main(String[] args) {         //无参构造函数         Student stu1= new Student();         stu1.show();         //一参构造函数         stu1=new Student("牛牛");         stu1.show();         //两参构造函数         stu1=new Student("牛牛",16);         stu1.show();         //修改年龄和姓名         stu1.setAge(20);         stu1.setName("牛可");         stu1.show();                  //不能输入错误的年龄         stu1.setAge(-20);         stu1.show();                  //不能输入错误的姓名         stu1.setName("");         stu1.show();     } } 2、Undergraduate类 /**  * 牛客网第三题——研究生类  */ package com.finersoft.nowcoder; /**  * 名称:研究生 说明:  *   * @author Finersoft  * @date 2018年3月1日  */ public class Undergraduate extends Student {     /** 学位 */     private String degree;     /** * @return 学位信息 */     public String getDegree() {         return degree;     }     /** * @param degree 学位信息 */     public void setDegree(String degree) {         if (degree != null && degree != "") {             this.degree = degree;         } else {             System.out.println("学位不能为空");         }     }     /**      * 无参构造      */     public Undergraduate() {         this("李四", 22, "Java开发");     }     /**      * 单参构造      *       * @param name      *            研究生姓名      */     public Undergraduate(String name) {         this(name, 22, "Java开发");     }     /**      * 双参构造      *       * @param name      *            研究生姓名      * @param age      *            研究生年龄      */     public Undergraduate(String name, int age) {         this(name, age, "Java开发");     }     /**      * 完整参数构造      *       * @param name      *            研究生姓名      * @param age      *            研究生年龄      * @param degree      *            学位信息      */     public Undergraduate(String name, int age, String degree) {         super(name, age);         this.degree = degree;     }     /**      * 转换成字符串操作      *       * @see com.finersoft.nowcoder.Student#toString()      */     @Override     public String toString() {         return "研究生"+ super.toString() + "\r\n\t学位:" + this.getDegree();     }     /**      * 打印研究生信息      *       * @see com.finersoft.nowcoder.Student#show()      */     @Override     public void show() {         System.out.println(this.toString());     }     /**      * @param args      */     public static void main(String[] args) {         // 无参构造函数         Undergraduate stu1 = new Undergraduate();         stu1.show();         // 一参构造函数         stu1 = new Undergraduate("牛牛");         stu1.show();         // 两参构造函数         stu1 = new Undergraduate("牛牛", 23);         stu1.show();         // 完整参数构造函数         stu1 = new Undergraduate("牛客", 24, "Java架构");         stu1.show();         // 修改学位信息         stu1.setDegree("JavaEE");         stu1.show();         // 修改学位信息——报错,学位不能为空         stu1.setDegree("");         stu1.show();     } } 3、 TestStudent类 /**  * 牛客网第三题——测试类  */ package com.finersoft.nowcoder; /**  * 名称:测试学生类  * 说明:  * @author Finersoft  * @date 2018年3月1日  */ public class TestStudent {     /**      * @param args      */     public static void main(String[] args) {         Student stu = new Student("牛珂",18);         stu.setAge(20);         stu.setName("牛可可");         stu.show();                  Undergraduate stu2= new Undergraduate(stu.getName(),stu.getAge(),"Java虚拟机");         stu2.show();     } }
点赞 回复 分享
发布于 2018-03-01 22:04
class Student {     protected String name;//姓名     protected int    age;//年龄      private static int num = 0;     public Student(String name,int age)     {         if(name != null)             this.name = name;         else             this.name ="Student " + num++;         if(age >= 0)             this.age  = age;         else             this.age  = 0;     }     public void show()     {         System.out.println(this.name + "的年龄是:" + this.age);     } } class Undergraduate extends Student {     private  String degree;//学位     public Undergraduate(String name,int age,String degree)     {         super(name,age);         this.degree = degree;     }     public void show()     {         System.out.println(this.name + "的年龄是:" + this.age + ",学位是:" + this.degree);     } } public class Test {     public static void main(String[] args)     {         Student s1 = new Student("小明",23);         Student s2 = new Student("小米",36);         Student s3 = new Student("小亮",12);         Student s4 = new Undergraduate("小红",37,"博士");         Student s5 = new Undergraduate("小辉",25,"本科");                  s1.show();         s2.show();         s3.show();         s4.show();         s5.show();     } }
点赞 回复 分享
发布于 2018-02-27 12:52
/**  * 设计一个学生类Student和它的子类Undergradute,要求如下:  * 1.Student类有name(姓名)和age(年龄),一个包含两个参数的构造方法,  * 用于给name和age属性赋值,一个show()方法dayinStudent的属性信息。  * @author FeiEr  *  */ public class Student {     String name;     int age;          public Student(String name,int age) {         this.name = name;         this.age = age;     }          public void show() {         System.out.println("我是"+name+","+"今年"+age+"岁!");     }      } /**  * 2.本科生类Undergraduate增加一个degree(学位)属性。有一个包含三个  * 参数的构造方法,前两个参数用于给继承的name和age赋值,第三个参数给degree专业赋值,  * 一个show()方法打印Undergrate的属性信息。  * @author FeiEr  *  */ public class Undergraduate extends Student {     private String degree;          public Undergraduate(String name, int age, String degree) {         super(name, age);         this.degree = degree;     }          public void show() {         System.out.println("我是"+name+","+"今年"+age+"岁"+","+degree+"毕业!");     }      } /**  * 3.在测试类中创建Student对象和Undergraduate的属性信息。  * @author FeiEr  *  */ public class Test001 {     public static void main(String[] args) {         Student student = new Student("王飞",24);         Undergraduate undergraduate = new Undergraduate("王剑",23,"硕士");         student.show();         undergraduate.show();     }      }
点赞 回复 分享
发布于 2018-02-27 21:31
public class Student { public int age; //学生年龄 public String name; //学生姓名 public String degree; //学生学位 public void Student(int StudentAge, String StudentName)//得到姓名和年龄 { this.name = StudentName; this.age = StudentAge; } public static class Undergraduate extends Student{ public void Undergraduate(int StudentAge, String StudentName,String StudentDegree) { this.name = StudentName; this.age = StudentAge; this.degree = StudentDegree; } } public void show() //打印学生信息 { System.out.println("名字:"+this.name+"\n年龄:"+this.age+"\n学位:"+this.degree); } }
点赞 回复 分享
发布于 2018-03-01 21:08
Studnet类 package entity; public class Student { public String name; //姓名 public Integer age; //年龄 //无参构造函数 public Student() {} //含参构造函数 public Student(String name, Integer age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } //打印学生信息 public void Show() { System.out.println("姓名:"+this.getName()+" 年龄:"+this.getAge()); } } Undergrated类 package entity; public class Undergraduate extends Student { public String degree; //学位 public Undergraduate(String name,Integer age,String degree) { super(name,age); this.degree = degree; } public String getDegree() { return degree; } public void setDegree(String degree) { this.degree = degree; } public void Show() { System.out.println( "姓名:"+this.getName()+" 年龄:"+this.getAge()+" 学位:"+this.getDegree()); } } 测试类 import entity.Student; import entity.Undergraduate; public class main { public static void main(String[] args) { // TODO 自动生成的方法存根 Student student1=new Student("张三",25); student1.Show(); Undergraduate undergraduate1=new Undergraduate("李四", 20, "博士"); undergraduate1.Show(); } }
点赞 回复 分享
发布于 2018-03-08 15:43
class Student { String name; int age; public Student(){} public Student(String getname,int getage) { name = getname; age = getage; } void show() { System.out.println("我的名字叫"+name+",今年"+age+"岁!"); } } class Undergraduate extends Student { private String degree; public Undergraduate(String getname,int getage,String getdegree) { name = getname; age = getage; degree = getdegree; } void show() { System.out.println("我的名字叫"+name+",今年"+age+"岁!来自"+degree+"专业。"); } } public class test01 { public static void main(String [] args) { Student stu = new Student("小明",5); Undergraduate und = new Undergraduate("小红",6,"通信工程"); stu.show(); und.show(); } }
点赞 回复 分享
发布于 2018-02-27 11:34
class Student {  protected String name;  protected int age;  public Student(String name,int age){   this.name=name;   this.age=age;  }  public void show(){   System.out.println("学生的名字为:"+this.name+",学生的年龄为:"+this.age);  } } class Undergrdute extends Student {  private String degree;  public Undergrdute(String name,int age,String degree){   super(name,age);   this.degree=degree;  }  public void show(){   System.out.println("学生的名字为:"+super.name+   ",学生的年龄为:"+super.age+   ",学生的专业为:"+this.degree);  } } public class Test{  public static void main(String[] args){   Student s1 = new Student("小明",20);   Student s2 = new Undergrdute("小辉",25,"计算机");   s1.show();   s2.show();  } }
点赞 回复 分享
发布于 2018-02-27 15:32
public class test { public static void main(String[] args) { Student stu1 =new Student("学炜",24); stu1.show(); Student stu2=new Undergradute("学炜",24,"硕士"); stu2.show(); } } public class Student {          String name;     int age;              public Student() {         super();     }     public Student(String name, int age) {         super();         this.name = name;         this.age = age;     }          public void show(){                 System.out.println("学生的名字为:"+this.name+" 学生的年龄为:"+this.age);     }          } public class Undergradute extends Student{     private  String degree;//学位     public Undergradute() {         super();             }     public Undergradute(String name, int age,String degree) {         super(name, age);         this.degree=degree;     }          public void show(){         System.out.println("学生的名字为:"+this.name+" 学生的年龄为:"+this.age+" 学生的专业为:"+this.degree);     }           }
点赞 回复 分享
发布于 2018-02-27 16:33
package student; class Student{     //父类属性访问权限设置为protected,可供子类访问     protected String name;     protected int age;     public Student(String name,int age)     {         this.name=name;         this.age=age;     }     public void show()     {         System.out.print("姓名:"+this.name+" ");         System.out.print("年龄:"+this.age);     } } class Undergraduate extends Student{          private String degree;          public Undergraduate(String name,int age,String degree)     {         super(name,age);//使用super()函数来调用父类相应的构造函数;         this.degree=degree;     }     public void show()     {         System.out.print("姓名:"+this.name+" ");         System.out.print("年龄:"+this.age+" ");         System.out.print("学位:"+this.degree);     } }  public class StudentTest {          public static void main(String[] args)     {         System.out.println("===学生类测试===");         Student s=new Student("李明",22);         s.show();         System.out.println();         System.out.println("===本科生类测试===");         s=new Undergraduate("张三",23,"学士学位");         s.show();     } }
点赞 回复 分享
发布于 2018-02-27 21:06
package Student; public class Student {     protected String name;     protected int age;          public Student(String name, int age){         this.name = name;         this.age = age;     }          public void show() {         System.out.println("姓名:" + this.name + ";" + "年龄:" + this.age);     }          public static void main(String[] args){         Student student = new Student("小红", 19);         Undergraduate unstudent = new Undergraduate("小明", 18, "计算机");         student.show();         unstudent.show();     } } class Undergraduate extends Student{     private String degree;          public Undergraduate(String name, int age, String degree){         //调用父类构造函数         super(name, age);         this.degree = degree;     }     //重写父类show方法     @Override     public void show() {         System.out.println("姓名:" + this.name + ";" + "年龄:" + this.age + ";" + "专业:" + this.degree);     } }
点赞 回复 分享
发布于 2018-02-28 21:37
public class Test {     public static void main(String[] args) {         Student student = new Student("肖捷", 28);         student.show();         Undergraduate undergraduate = new Undergraduate("***", 29, "经济学");         undergraduate.show();     } } class Student {     private String name;     private int age;     Student (String name, int age){         this.name = name;         this.age = age;     }     protected void show() {         System.out.println("学生名字是:" + name);         System.out.println("该学生的年龄是:" + age);     } } class Undergraduate extends Student{     private String degree;     Undergraduate(String name, int age, String degree) {         super(name, age);         this.degree = degree;     }          protected void show() {         super.show();         System.out.println("该学生的学位是:" + degree);     } }
点赞 回复 分享
发布于 2018-03-01 09:38
public class ExtendsTest {     /**      * 继承关系测试      */     public static void main(String[] args) {         Student st=new Student("zhangSan",18);//实例化父类对象         st.show();//利用对象调用方法         Undergraduate ug=new Undergraduate("liSi",20,"undergraduate");         ug.show();     } } /*  * 定义学生类  * 属性:姓名 年龄   * 方法:输出属性  */ class Student{        String name;      int age;     public Student(String name,int age){         this.name=name;         this.age=age;     }     public void show(){         System.out.println("姓名:"+name+" 年龄:"+age);     } } /*  * 定义本科生子类  * 增加新属性:专业  */ class Undergraduate extends Student{     String degree=null;     public Undergraduate(String name,int age, String degree){         super(name,age);//调用父类构造函数         this.degree=degree;     }     public void show(){         System.out.println("姓名:"+name+" 年龄:"+age+" 专业:"+degree);     } }
点赞 回复 分享
发布于 2018-03-01 17:02
public class Undergraduate extends Student { private String degree; public Undergraduate(String name, int age, String degree) { super(name, age); this.degree = degree; } @Override public void show() { super.show(); System.out.println("学位:" + degree); } } public class Student { private String name; private int age; public Student(String name, int age) { this.name = name; this.age = age; } public void show(){ System.out.println("姓名:" + name + "\n年龄:" + age); } } public class TestStudent { public static void main(String[] args) { Student s1 = new Student("yhviews", 20); Undergraduate u1 = new Undergraduate("zeal", 22, "本科"); s1.show(); u1.show(); } }
点赞 回复 分享
发布于 2018-03-01 22:16
点赞 回复 分享
发布于 2018-03-01 22:20
package com.nowcode.www.demo03; public class Student {     protected String name;     protected int age;     public Student(){              }     public Student(String name,int age){         super();         this.name=name;         this.age=age;     }     public void show(){         System.out.println("name:"+name+"age:"+age);     } } class Undergraduate extends Student{     private String degree;     public Undergraduate(){              }     public Undergraduate(String name, int age,String degree) {         super(name, age);         this.degree=degree;     }     public void show(){         System.out.println("name:"+name+"age:"+age+"degree:"+degree);     } }
点赞 回复 分享
发布于 2018-03-02 08:00
class Student{          private String name;     private int 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;     }     /**      * @param name      * @param age      */     public Student(String name, int age) {         super();         this.name = name;         this.age = age;     }          public Student() {         // TODO Auto-generated constructor stub     }          public void show() {         System.out.println("Student [name=" + name + ", age=" + age + "]");     } } class Undergraduate extends Student{          private String degree;     public String getDegree() {         return degree;     }     public void setDegree(String degree) {         this.degree = degree;     }     /**      * @param name      * @param age      * @param degree      */     public Undergraduate(String name, int age, String degree) {         super(name, age);         this.degree = degree;     }     @Override     public void show() {         System.out.println("Undergraduate [ degree=" + degree + "]");     } } public class TestStudent3 {     public static void main(String[] args) {         Student student1 = new Student("张三", 12);         Student student2 = new Student("李四", 13);         student1.show();         student2.show();         Undergraduate undergraduate = new Undergraduate("王五", 20, "硕士");         undergraduate.show();     } }
点赞 回复 分享
发布于 2018-03-04 22:08
class Student { String name; int age; public Student(String co_name,int co_age){ name = co_name; age = co_age;   }   public void show(){ System.out.println("我的名字是"+name+":"+age+"years old!");   } } class Undergradute extends Student{ String degree; public Undergradute(String co_name,int co_age,String getDegree){ super(co_name,co_age);   degree = getDegree; } public void show(){ System.out.println("我是"+name+",今年"+age+"岁,学位是"+degree); } } class Test{ public static void main(String[] args){ Student stu1 = new Student("明仔",23);   Student stu2 = new Student("红红",22);   Student stu3 = new Undergradute("继伟",30,"本科");   Student stu4 = new Undergradute("小霞",28,"研究生");   stu1.show();   stu2.show(); stu3.show(); stu4.show(); } }
点赞 回复 分享
发布于 2018-03-05 22:20
class Student {     String name;     int age;          public void Student(String name,int age) {         this.name = name;         this.age = age;     }          public void show () {         System.out.println("学生姓名:"+this.name+" 学生年龄:"+this.age);     } } class Undergraduate extends Student {     String degree;          public void Undergraduate(String name,int age,String degree) {         super.Student(name,age);         this.degree = degree;     }          public void show () {         System.out.println("学生姓名:"+this.name+" 学生年龄:"+this.age+" 学生学位:"+this.degree);     } } public class StudentDemo {     public static void main(String[] args) {         Student s = new Student();         s.Student("小红",24);         s.show();                  Undergraduate u = new Undergraduate();         u.Undergraduate("小红",24,"硕士");         u.show();     } }
点赞 回复 分享
发布于 2018-03-07 11:29
public class Student {     public int age;     public String name;     public Student(String name,int age)     {      this.name=name;      this.age=age;     }  public void show() {   System.out.println("This student is "+this.name +", age is "+this.age);  } } public class Udergradute extends Student {  String name;  int age;  String degree;  public Udergradute(String name, int age,String degree) {   super(name, age);   this.name=name;   this.age=age;   this.degree=degree;   // TODO Auto-generated constructor stub  }  public void show() {   System.out.println("This undergradute is "+this.name +",age is "+this.age+",degree is"     + " "+this.degree);  } } public class test {  public static void main(String[] args) {  Student s1=new Student("Lemon",18);  Udergradute s2=new Udergradute("Sissy",23,"doctor");  s1.show();  s2.show();  } }
点赞 回复 分享
发布于 2018-03-09 10:26
public class Test_3 {     public static void main(String[]args) {         Student st=new Student("张三",18);         Undergradute un=new Undergradute("李四",20,"硕士");         st.show();         un.show();     } } class Student{     protected String name;     protected int age;     public Student(String name,int age) {         this.name=name;         this.age=age;     }     public void show() {         System.out.println("姓名:"+name+','+"年龄:"+age);     } } class Undergradute extends Student{     protected String degree;     public Undergradute(String name, int age, String degree) {         super(name,age);         this.degree=degree;     }     public void show() {         System.out.println("姓名:"+name+','+"年龄:"+age+','+"学位:"+degree);     } }
点赞 回复 分享
发布于 2018-03-12 19:29

相关推荐

头像
11-04 11:19
中南大学 Java
哇为,遥遥领先
求你了offer啊啊啊啊啊啊啊啊啊啊:我的评价是华为比亚迪是每个混子的温柔乡
点赞 评论 收藏
分享
大拿老师:这个简历,连手机号码和照片都没打码,那为什么关键要素求职职位就不写呢? 从上往下看,都没看出自己到底是产品经理的简历,还是电子硬件的简历? 这是一个大问题,当然,更大的问题是实习经历的描述是不对的 不要只是去写实习流程,陈平,怎么去开会?怎么去讨论? 面试问的是你的产品功能点,是怎么设计的?也就是要写项目的亮点,有什么功能?这个功能有什么难处?怎么去解决的? 实习流程大家都一样,没什么优势,也没有提问点,没有提问,你就不得分 另外,你要明确你投的是什么职位,如果投的是产品职位,你的项目经历写的全都是跟产品无关的,那你的简历就没用 你的面试官必然是一个资深的产品经理,他不会去问那些计算机类的编程项目 所以这种四不像的简历,在校招是大忌
点赞 评论 收藏
分享
冷艳的小师弟在看机会:jd测评乱点直接被挂了,哭死~
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务