① 定义学生类,基于学生类生成一个对象,并打印该对象所有属性。
public class hw1009 {
public static class Student {
String name;
char gender;
int age;
int gra;
int cls;
double high;
public void fd(int x,int y) {
System.out.print("班级:"+x);
System.out.print("年");
System.out.print(y);
System.out.print("班");
}
public static void main(String[] args) {
Student s1;
s1=new Student();
s1.name="李华";
s1.gender='男';
s1.gra=9;
s1.cls=6;
s1.high=1.89;
System.out.println("姓名:"+s1.name+"\n性别:"+s1.gender+"\n身高:"+s1.high+"米");
s1.fd(s1.gra,s1.cls);
}
}
}
② 创建一个无形参构造方法的学生类,生成对象,并打印对象的属性。(1011)
public class Student1 {
public int age;
public String name;
public String major;
public String id;
public String phone;
Student1( ){
}
void printInf(){
System.out.println("姓名:"+name+" 年龄:"+age+" 专业:"+major+" 学号:"+id+" 电话:"+phone);
}
public static void main(String[] args) {
Student1 s1;
s1=new Student1();
s1.name="李华";
s1.age=19;
s1.major="计算机";
s1.id="23111111";
s1.phone="18912345678";
s1.printInf();
}
}
③ 创建一个有形参构造方法的学生类,同时定义一个方法打印所有属性,基于该类生成对象,并打印对象的属性。(1011)
public class Student2 {
public int age;
public String name;
public String major;
public String id;
public String phone;
Student2(int age1,String name1,String m1, String id1,String num1){
age=age1;
name=name1;
major=m1;
id=id1;
phone=num1;
}
void printInf(){
System.out.println("姓名:"+name+" 年龄:"+age+" 专业:"+major+" 学号:"+id+" 电话:"+phone);
}
public static void main(String[] args) {
Student2 s1;
s1=new Student2(19,"李华","计算机","23111111","18912345678");
s1.printInf();
}
}
④ 定义Shape类,并分别生成圆、正三角形,正方形对象,计算他们的周长(girth)。(使用方法的重载)
package t1017;
public class Space {
double l;
String XZ;
double girth(double l1,String x1){
double gir=0;
if(x1=="正方形"){
gir=4*l1;
}else if(x1=="正三角形"){
gir=3*l1;
}else if(x1=="圆形"){
gir=Math.PI*2*l1;
}
return gir;
}
public static void main(String[] args) {
Space s1;
s1=new Space();
s1.l=5;
s1.XZ="圆形";
System.out.println("圆周长:"+s1.girth(s1.l, s1.XZ));
Space s2;
s2=new Space();
s2.l=10;
s2.XZ="正方形";
System.out.println("正方形周长:"+s2.girth(s2.l,s2.XZ));
Space s3;
s3=new Space();
s3.l=10;
s3.XZ="正三角形";
System.out.println("正三角形周长:"+s3.girth(s3.l,s3.XZ));
}
}
⑤ 定义一个类,放在test1目录下。该类有一个方法,计算2个整数之和。
package test1;
public class Lei {
public static int sum(int a, int b) {
return a + b;
}
public static void main(String args[]) {
System.out.println("两个整数的和为:" + sum(1,6));
}
}
定义另外一个类,放在test2目录下,调用刚才定义的类,生成对象,并调用计算2个整数之和的方法,打印计算的结果。
package text2;
import test1.Lei;
public class Test2 {
public static void main (String[] args) {
Lei t1 =new Lei();
int s = Qw.sum(5,6);
System.out.println("两个整数的和为:" + s);
}
}