用java编写一个简单例子,题目如下

编写一个student类,其中包括(姓名、学号、班级、年龄、家庭住址)和取得这些
属性的方法,再增加一个sayHello()方法,用于输出学生的详细信息(1)创建Student
类的对象,编写程序测试这些方法。(2)创建两个学生对象,并比较二者的年龄,输出
年龄较大者的学生信息

package test;

public class Student {
private String name;
private String id;
private String clazz;
private int age;
private String address;

/**
* sayHello方法
*/
public void sayHello() {
System.out.println("学号为" + this.id + "的同学的具体信息如下:");
System.out.println("姓名:" + this.name);
System.out.println("班级:" + this.clazz);
System.out.println("年龄:" + this.age);
System.out.println("家庭住址:" + this.address);
}

/**
* 测试方法
*
* @param args
*/
public static void main(String[] args) {
// 第一问
Student student = new Student();
student.setAddress("百度知道");
student.setAge(1);
student.setClazz("一班");
student.setId("071251000");
student.setName("lsy605604013");
student.sayHello();

// 第二问
Student studentNew = new Student();
studentNew.setAddress("搜搜知道");
studentNew.setAge(2);
studentNew.setClazz("二班");
studentNew.setId("071251001");
studentNew.setName("lady");
if (student.getAge() < studentNew.getAge())
studentNew.sayHello();
else if (student.getAge() > studentNew.getAge())
student.sayHello();
else
System.out.println("两人一样大");

}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getClazz() {
return clazz;
}

public void setClazz(String clazz) {
this.clazz = clazz;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

public String getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address;
}

}
温馨提示:内容为网友见解,仅供参考
第1个回答  2021-12-21

java:

      @Test
    void contextLoads() {
    Student student = new Student ("张三","1","三年级四班",18,"xxx");
    Student student2 = new Student ("张三","1","三年级四班",10,"xxx");
    System.out.println (sayHello(student,student2));
    }

    private Student sayHello(Student student, Student student2) {
    return  student.getAge ()>student2.getAge ()? student:student2;
    }

    //    编写一个student类,其中包括(姓名、学号、班级、年龄、家庭住址)和取得这些
    //    属性的方法,再增加一个sayHello()方法,用于输出学生的详细信息(1)创建Student
    //    类的对象,编写程序测试这些方法。(2)创建两个学生对象,并比较二者的年龄,输出
    //            年龄较大者的学生信息
    }
    @Data
    class Student{
    private String name; //姓名
    private String stuId; //学号
    private String stuClass;//班级
    private Integer age;//年龄
    private String homeAddr;//住址

    public Student() {
    }

    public Student(String name, String stuId, String stuClass, Integer age, String homeAddr) {
    this.name = name;
    this.stuId = stuId;
    this.stuClass = stuClass;
    this.age = age;
    this.homeAddr = homeAddr;
    }

    @Override
    public String toString() {
    return "Student{" +
    "name='" + name + '\'' +
    ", stuId='" + stuId + '\'' +
    ", stuClass='" + stuClass + '\'' +
    ", age=" + age +
    ", homeAddr='" + homeAddr + '\'' +
    '}';
    }
    }

第2个回答  2012-01-19
/**
* student类
*
*/
public class Student {
private String name;
private String id;
private String clazz;
private int age;
private String address;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getClazz() {
return clazz;
}
public void setClazz(String clazz) {
this.clazz = clazz;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Student() {

}
public Student(String name, String id, String clazz, int age,
String address) {
this.name = name;
this.id = id;
this.clazz = clazz;
this.age = age;
this.address = address;
}
public void sayHello(){
System.out.println(getName()+"的详细信息如下:");
System.out.println("学号:"+getId());
System.out.println("班级:"+getClazz());
System.out.println("年龄:"+getAge());
System.out.println("地址:"+getAddress());
}
}

/**
* 测试类
*
*/
public class Test {
public static void main(String[] args) {
// Student st1 = new Student("张三", "0000001", "一班", 15, "张三家");
// Student st2 = new Student("李四", "0000002", "二班", 16, "李四家");
Student st1 = new Student();
Student st2 = new Student();
st1.setName("张三");
st1.setId("000001");
st1.setClazz("一班");
st1.setAge(15);
st1.setAddress("张三家");
st2.setName("李四");
st2.setId("000002");
st2.setClazz("二班");
st2.setAge(16);
st2.setAddress("李四家");

if(st1.getAge()>st2.getAge()){
System.out.println(st1.getName()+"年龄比"+st2.getName()+"大,下面将输出"+st1.getName()+"的详细信息");
st1.sayHello();
}else if(st1.getAge()<st2.getAge()){
System.out.println(st2.getName()+"年龄比"+st1.getName()+"大,下面将输出"+st2.getName()+"的详细信息");
st2.sayHello();
}else {
System.out.println(st1.getName()+"和"+st2.getName()+"年龄相同");
}
}
}
第3个回答  2012-01-19
按照OO的思想,比较操作实现接口比较好。代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MyCSharpProjects
{
class Student:IComparable
{
#region private fields
private string name;
private string id;
private string grade;
private int age;
private string address;
#endregion

#region public properties
public string Name
{
get { return name; }
set { name = value; } // here you could apply pattern or regular expression to the name.
}
// ...其他属性类型,照抄即可。
#endregion

#region protected method
public override string ToString()
{
StringBuilder strbuilder = new StringBuilder();
strbuilder.Append("Name :");
strbuilder.AppendLine(Name);

// 其他属性找这个抄都写到strbuilder中

return strbuilder.ToString();
}

#endregion

#region public methods
public void SayHello()
{
this.ToString();
}

public override bool Equals(object obj)
{
if (CompareTo(obj) == 0)
return true;
return false;
}

public override int GetHashCode()
{
return base.GetHashCode();
}

public virtual int CompareTo(object obj)
{
Student s = obj as Student;

if (s != null)
{
if (s == this)
{
return 0;
}
else
{
if (s.Name == this.Name && s.id == this.id && s.grade == this.grade && s.age == this.age && s.address == this.address)
return 0;
else if (s.age > this.age)
{
return 1;
}
else
{
return -1;
}
}
}

throw new Exception("invalid type");
}

#endregion

}
}
第4个回答  2012-01-19
我能独自完成作业!这里我的定义和张老师的不同, 张老师是鼓励大家讨论的其实也就是两种方式吧, 一种就是用实例, 就是给你一个例子, 要你用

java趣味编程实例?
java趣味编程实例?用java编写一个简单例子,题目如下packagetest;publicclassStudent{privateStringname;privateStringid;privateStringclazz;privateintage;privateStringaddress;/***sayHello方法*/publi

用JAVA语言编写下面的题目: 一个数如果恰好等于它的因子之和,这个数称...
public class WanQuanShu { public static void main(String[] args) { int i,j,k; \/\/定义整型变量i,j,k 用于for循环 int []sector=new int[32]; \/\/定义整型数组用于存放完全数的每个因子 for(i=1;i<=1000;i++){ \/\/for循环寻找1000以内的完全数 int sum=1; \/\/...

java编程计算1+3!+5!+...+(2n-1)! 前10 项和。小弟编写的代码如下
sum1 = sum1 * j;} sum = sum + sum1;} System.out.println("1~19的奇数阶乘和为"+sum);首先你的循环中i=1在无限循环,要改成i++,就是每循环一次i递增1 然后你是要加到19为止,就必须是2*i-1<=19 然后 1! = 1 3! = 1*2*3 5! = 1*2*3*4*5 这些必须体现出来,也就...

怎样用JAVA编程编写题目:设计一个窗体,在窗体中有个按钮,单击退出按钮...
import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import javax.swing.JFrame;import javax.swing.JOptionPane;public class SimpleFrame {public static void main(String[] args) { MyFrame frame = new MyFrame();frame.setDefaultCloseOperation( JFrame.DO_NOTHING_ON_CLOSE);...

java编程,采用for循环,题目如下:
import java.util.*;public class Test6 { public static void main(String[] args){ Random R=new Random(101);int sum=0;for(int i=0;i<100;i++){ \/\/不能等于等于就循环101次了 int score=R.nextInt(101);sum=sum+score;System.out.println("第"+(i+1)+"个同学的成绩为:"+...

用java编写程序:求银行本息的!题目如下:
简单些了个,如果没理解错的话,应该可以满足要求:public class Benxi{ private double benxi;\/\/本息 private double lilu;\/\/年利率 \/\/计算本息 private double resBenxi(double money,int year){ benxi=money+money*getLilu(year)*year;return benxi;} \/\/选择利率 private double getLilu(int year...

用java编写一个简单例子,题目如下
package test;public class Student { private String name;private String id;private String clazz;private int age;private String address;\/ sayHello方法 \/ public void sayHello() { System.out.println("学号为" + this.id + "的同学的具体信息如下:");System.out.println("姓名:" + this....

java题目:创建一个部门类(Department),要求如下:
static void main(String[] args) { Department.setdMoney(300D);Department d1 = new Department("部门1", 20);Department d2 = new Department("部门2", 10);d1.consume(80);d2.consume(40);System.out.println("剩下的钱:" + Department.getdMoney());} } 输出结果:输出结果 ...

Java编程:请看题目,在下面,麻烦给出完整的解答。我是个菜鸟,谢谢
import java.util.Scanner;class Test { public static final String STR_INPUT_N = "请输入小明的自有话费(整数),N:"; public static final String STR_INPUT_M = "请输入每使用多少元就可以获赠1元,M:"; public static final String STR_INPUT_N_AGAIN = "请再次输入小明的自有话...

有关java编程题目?
按照题目要求编写的圆,圆锥和测试类的Java程序如下 Test.java文件内容如下 class Circle{ private double r;private String color;public Circle(double r){ this.r=r;} public double area(){ return Math.PI*r*r;} public double perimeter(){ return Math.PI*2*r;} public double getR(){ ...

相似回答