类和对象基础题2

编写Java应用程序。首先,定义描述学生的类——Student,包括学号(int)、

姓名(String)、年龄(int)等属性;二个方法:Student(int stuNo,String name,int age)

用于对对象的初始化,outPut()用于输出学生信息。其次,再定义一个主类——

TestClass,在主类的main方法中创建多个Student类的对象,使用这些对象来测

Student类的功能。

 

    int stuNo;
    String name;
    int age;
    Student(int stuNo,String name,int age)
    {
        this.stuNo=stuNo;
        this.name=name;
        this.age=age;
    }
    void outPut()
    {
        System.out.println("学号:"+stuNo+" "+"姓名:"+name+" "+"年龄:"+age);
    }

 

public class TestClass {

    public static void main(String[] args) {
        Student stu1=new Student(12,"张三",18);
        stu1.outPut();
        Student stu2=new Student(23,"李四",15);
        stu2.outPut();
        Student stu3=new Student(78,"王五",19);
        stu3.outPut();
        Student stu4=new Student(42,"赵六",17);
        stu4.outPut();

    }

}

编写一个Java应用程序,该应用程序包括2个类:Print类和主类EPrint

类里有一个方法output()功能是输出100 ~ 999之间的所有水仙花数(各位数字的

立方和等于这个三位数本身,如: 371 = 33 + 73 + 13。)在主类Emain方法中来

测试类Print

 

public class Print {

    
    void output()
    {
        for (int num=100;num<1000;num++)
        {
            int gw=num%10;
            int sw=num/10%10;
            int bw=num/100%10;
            if (gw*gw*gw+sw*sw*sw+bw*bw*bw==num)
            {
                System.out.println(num);
            }
        }
    }
    

}

 

public class E {

    public static void main(String[] args) {
        Print sum=new Print();
        
        sum.output();

    }

}

 

posted on 2016-05-17 15:03  煜渝  阅读(186)  评论(0编辑  收藏  举报

导航