对象的使用练习

人类属性:

需求:
这里写图片描述

类的定义

package Demo32;

public class Person {
    String name;
    double height;
    double weight;
    int age;
    String sex;

    public Person(String name, int age, String sex) {
        this.name = name;
        this.age = age;
        this.sex = sex;
    }

    public void show() {
        System.out.println("大家好!\n我叫" + name + ",今年" + age + "岁\n我是一个身高" + height + ",体重" + weight + "的" + sex);
    }

    public void work(String content) {
        System.out.println(name + "的工作理念是:" + content);
    }

    public Person() {
        System.out.println("我实例化了" + name + "对象");
    }
}

对象的操作

package Demo32;

public class PersonTest {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Person abao = new Person("阿宝", 25, "男人");
        abao.height = 175.0;
        abao.weight = 120.0;
        abao.show();
        abao.work("有钱就好");
    }

}

两数相加方法:

需求:写一个可重复执行的两数相加的程序。
    public Cal(int x, int y) {
        this.x1 = x;
        this.y1 = y;
    }

    public void Ad() {
        System.out.println(x1 + y1);

    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input = new Scanner(System.in);
        String goOn = "n";
        do {
            System.out.println("请输入第一个数");
            int First = input.nextInt();
            System.out.println("请输入第二个数");
            int Sceond = input.nextInt();
            Cal a = new Cal(First, Sceond);
            a.Ad();
            System.out.println("是否继续y/n");
            goOn = input.next();
        } while (goOn.equals("y"));
        System.out.println("程序结束");
    }

}
执行结果:

这里写图片描述


成绩总分查询:

需求:

这里写图片描述

package Day4;

import java.util.Scanner;

public class Demo2 {
    int jAva;
    int c;
    int Db;

    public Demo2(int a, int b, int c) {
        this.jAva = a;
        this.c = b;
        this.Db = c;
    }

    public int Add() {
        int sum = jAva + c + Db;
        return sum;
    }

    public double Arg() {
        double arg = Add() / 3;
        return arg;
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input = new Scanner(System.in);
        System.out.println("请输入您的java成绩");
        int jAva = input.nextInt();
        System.out.println("请输入您的c#成绩");
        int c = input.nextInt();
        System.out.println("请输入您的DB成绩");
        int Db = input.nextInt();
        Demo2 score = new Demo2(jAva, c, Db);
        int sum = score.Add();
        double arg = score.Arg();
        System.out.println("您的总成绩为:" + sum);
        System.out.println("您的平均成绩为:" + arg);

    }

}

管理员登陆系统

需求:(扩展:类似于登陆界面的判定)
这里写图片描述

package Day4;

import java.util.Scanner;

public class Demo3 {

    String Name;
    int passWd;

    public Demo3(String name, int passwd) {
        this.Name = name;
        this.passWd = passwd;
    }

    public void show() {
        if ((Name.equals("gujunhua")) && (passWd == 123456)) {
            System.out.println("登录成功");
        } else {
            System.out.println("账户名或者密码输入错误");
        }
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input = new Scanner(System.in);
        System.out.println("请输入您的管理员姓名:");
        String name = input.next();
        System.out.println("请输入您的密码:");
        int passwd = input.nextInt();
        Demo3 massager = new Demo3(name, passwd);
        massager.show();

    }

}

结果:
这里写图片描述
这里写图片描述


购物管理系统:

需求:做几个页面跳转的判定及方法

这里写图片描述
代码:

package Day4;

import java.util.Scanner;

public class Demo4 {
    public void custom() {
        Scanner input = new Scanner(System.in);
        System.out.println("欢迎来到我行我素购物管理系统");
        System.out.println("1.登录系统");
        System.out.println("2.退出");
        System.out.println("--------------------------");
        System.out.println("请选择,输入数字:");
        int key = input.nextInt();
        if (key == 1) {
            login();
        } else {
            System.out.println("程序结束");
        }
    }

    public void login() {
        Scanner input = new Scanner(System.in);
        System.out.println("我行我素购物管理系统主菜单");
        System.out.println("--------------------------");
        System.out.println("1.客户信息管理");
        System.out.println("2.真情回馈");
        System.out.println("--------------------------");
        System.out.println("请选择,输入数字1或按0返回上一界面:");
        int key = input.nextInt();
        if (key == 1 || key == 0) {
            custom();
        } else if (key == 2) {
            love();
        }

    }

    public void love() {
        Scanner input = new Scanner(System.in);
        System.out.println("我行我素购物管理系统> 真情回馈");
        System.out.println("--------------------------");
        System.out.println("1.幸运大放送");
        System.out.println("2.幸运抽奖");
        System.out.println("3.返回上层");
        int key = input.nextInt();
        if (key == 3) {
            login();
        }

    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Demo4 custom = new Demo4();
        custom.custom();
    }

}

多个类的使用

需求为:建立两个类,加一个测试类.输出gujunhua的身高.年龄,体重之和.一个文件封装对象属性,一个封装对象的方法.一个用来测试.

package test;

public class People {
    int age;
    int height;
    int weight;
    public People(int Age,int Height,int Weight){
        this.age = Age;
        this.height = Height;
        this.weight = Weight;
    }
}
package test;

public class PeopleAge {
    public int addAge(People people) {
        int sum = people.age + people.weight + people.height;
        System.out.println(sum);
        return 0;
    }
}
package test;

public class PeopleTest {
    public static void main(String[] args) {
        People gujunhua = new People(22, 170, 130);
        PeopleAge add = new PeopleAge();
        add.addAge(gujunhua);
    }
}

这里写图片描述

对象数组的使用

实例:
需求:求5个工人的平均身高

package test;
//work类的定义
public class Worker {
    double height;

    public Worker(double height) {
        this.height = height;
    }
}
package test;
//对方法类的定义
public class WorkAdd {
    public int addHeight(Worker[] worker) {
        int sum = 0;
        for (int i = 0; i < worker.length; i++) {
            sum += worker[i].height;
        }
        int arg = sum / worker.length;
        System.out.println("5个工人的平均身高为" + arg);
        return 0;
    }
}
package test;
//对主函数类的定义
import java.util.Scanner;

public class WorkTest {
    public static void main(String[] args) {
        Worker[] worker = new Worker[5];
        Scanner input = new Scanner(System.in);
        for (int i = 0; i < worker.length; i++) {
            System.out.println("输入第" + (i + 1) + "工人的身高");
            worker[i] = new Worker(worker.length);
            double height = input.nextDouble();
            worker[i].height = height;
        }
        WorkAdd add = new WorkAdd();
        add.addHeight(worker);
    }
}

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值