类和对象(3)

文章展示了几个Java编程实例,包括创建Person对象并调用成员方法,实现石头剪刀布游戏的逻辑,声明MyDate和Employee类并处理日期和员工信息,以及方法重载的概念和应用。同时,还包含了数组增长和三角形面积计算的不同方法。
摘要由CSDN通过智能技术生成

1、   //创建一个对象:名叫“张三”,性别“男”,年龄18岁,身高1.80;

        // 让该对象调用成员方法:

        // 说出“你好!”

        // 计算23+45的值

        // 将名字改为“李四”

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

    public void sayHello() {
        System.out.println(name + "说你好");
    }

    public int calculate(int a, int b) {
        int c = a + b;
        return c;
    }
}
public class mainTest {
    public static void main(String[] args) {
        Person zs = new Person();
        zs.name = "张三";
        zs.sex = "男";
        zs.age = 18;
        zs.height = 1.8;

        zs.sayHello();

        int i = zs.calculate(23, 45);
        System.out.println(i);

        zs.name = "李四";
        zs.sayHello();

    }
}

2、调用方法实现剪刀石头布

import java.util.Random;

/**
 * 电脑对象
 * //姓名
 * //分数
 */
public class Computer {
    String name;
    int score;

    public int chuquan(){
        Random rand = new Random();
        int key = rand.nextInt(3)+1;
        switch (key){
            case 1:
                System.out.println(name +"出的是石头");
                break;
            case 2:
                System.out.println(name +"出的是剪刀");
                break;
            case 3:
                System.out.println(name +"出的是布");
                break;
        }
        return key;
    }
}
import java.util.Scanner;

/**
 * 用户对象
 * //姓名
 * //分数
 */
public class Person {
    String name;
    int score;

    public int chuquan(){
        Scanner input = new Scanner(System.in);
        System.out.println("请"+name+"出拳");
        int key =input.nextInt();
        switch (key){
            case 1:
                System.out.println(name + "出的是石头");
                break;
            case 2:
                System.out.println(name + "出的是剪刀");
                break;
            case 3:
                System.out.println(name + "出的是布");
                break;
        }
        return key;
    }
}
import java.util.Scanner;

public class Test {//石头剪刀布

    public static void main(String[] args) {//主函数值调用游戏开始函数
        Test tt = new Test();
        tt.gameStart();

    }

    /**
     * 游戏开始
     */
        Computer com = new Computer();
        Person per = new Person();
    public void gameStart() {
        Scanner input = new Scanner(System.in);
        System.out.println("请给自己起个昵称:");
        per.name = input.next();
        System.out.println("请选择你的对战角色:1、刘备\t2、董卓\t3、诸葛亮");
        int xz =input.nextInt();
        switch (xz){
            case 1:
                com.name = "刘备";
                break;
            case 2:
                com.name = "董卓";
                break;
            case 3:
                com.name = "诸葛亮";
                break;
        }
        do {
        int person = per.chuquan();//用户出拳
        int computer = com.chuquan();//电脑出拳
        result(person,computer);//比较结果
        }while (com.score<3 && per.score<3);
        showresult();
    }

    /**
     * 胜负判定
     */
    public void result(int person,int computer) {
        if (person == 1 && computer == 2 || person == 2 && computer == 3 || person == 3 && computer == 1) {
            per.score++;
            System.out.println(per.name + "本局胜利");
            System.out.println("*************************************");
        } else if (person == computer) {
            System.out.println("平局,双方继续");
            System.out.println("*************************************");
        } else {
            com.score++;
            System.out.println(com.name + "本局胜利");
            System.out.println("*************************************");
        }
    }

    public void showresult(){
        System.out.println("-------------------------------------------------");
        System.out.println("--------"+per.name+"\t\t\tVS\t\t\t"+com.name+"--------");
        System.out.println("-------------------------------------------------");
        System.out.println("--------"+per.score+"\t\t\t\tVS\t\t\t\t"+com.score+"--------");
        System.out.println("-------------------------------------------------");
        if (per.score>com.score){
            System.out.println("恭喜"+per.name+"获得最终胜利");
        }else if (per.score == com.score){
            System.out.println("平局,下次继续");
        }else{
            System.out.println("恭喜"+com.name+"获得最终胜利");
        }
    }
}

3、声明1个MyDate类型

//有属性:年,月,日

//增加一个String getDateInfo()方法,用于返回日期对象信息,例如:xx年xx月xx日

声明另一个Employee类型

//有属性:姓名(String类型),生日(MyDate类型)

//增加一个void setBirthday(int year,int month,int day)方法,用于给员工生日赋值

//增加一个String getEmpInfo()方法,用于返回员工对象信息,例如:姓名:xx,生日:xx年xx月xx日

//在测试类中,调用函数为员工生日赋值,并显示

public class myDate {
    int year;
    int month;
    int day;


    public String getDateInfo(){
        return year +"年" +month +"月"+day +"日";
    }

}
import java.util.Scanner;

public class Employee {

    String  name;
    myDate birthday;

    public String setBirthday(){
        Scanner input = new Scanner(System.in);
        myDate myDate = new myDate();
        System.out.println("请输入年");
        myDate.year = input.nextInt();
        System.out.println("请输入月");
        myDate.month= input.nextInt();
        System.out.println("请输入日");
        myDate.day= input.nextInt();
        String s = myDate.getDateInfo();
        return s;
    }

    public String getEmpInfo(){
        Scanner input = new Scanner(System.in);
        System.out.println("请输入员工姓名:");
        name = input.next();
        String x = "姓名:" + name + "\t" + setBirthday();
        return x;
    }

}
public class mainTest {
    public static void main(String[] args) {
        Employee employee = new Employee();
        String s1 = employee.getEmpInfo();
        System.out.println(s1);
    }
}

4、重载知识点:

public class chongZai {
    /*
         方法参数传递为引用数据类型 :

                传入方法中的, 是内存地址.
     */
    public static void main(String[] args) {
        int[] arr = {10, 20, 30};
        System.out.println("调用change1方法前:" + arr[1]);
//        调用方法时,传入的是地址,所以方法结束后,对应地址上的值改变
        change1(arr);
        System.out.println("调用change1方法后:" + arr[1]);

        int number = 100;
        System.out.println("调用change方法前:" + number);
        int i = change(number);
//        调用有返回值类型的方法,返回的是方法里面产生的值,则对应的值改变
        System.out.println("调用change方法后:" + i);
        change2(number);
//        调用无返回类型的方法,传入的参数是值,方法结束后,不影响原值,则值不变
        System.out.println("调用change2方法后" + number);
    }

    public static void change1(int[] arr) {//引用数据类型
        arr[1] = 200;
    }
        /*
             方法参数传递为基本数据类型 :

                    传入方法中的, 是具体的数值.
         */
    public static int change(int number) {//基本数据类型
        number = 200;
        return number;
    }

    public static void change2(int number){
        number = 200;
    }
}

5、方法重载练习

import java.util.Scanner;

public class Test1 {//练习方法重载

    //简易计算器
    //实现任意两个数相加,包括浮点数及整数
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("输入第一个小数");
        double a = input.nextDouble();
        System.out.println("输入第二个小数");
        double b = input.nextDouble();
        System.out.println("输入第一个整数");
        int c = input.nextInt();
        System.out.println("输入第二个整数");
        int d = input.nextInt();
        double add = ADD(a, b);
        double Add = ADD(c, d);
        System.out.println(add);
        System.out.println(Add);
    }

    public static double ADD(int a, int b) {
        double c = a + b;
        return c;
    }

    public static double ADD(double a, double b) {
        double c = a + b;
        return c;
    }
}

6、   //数组长度扩大2倍

        //方法1:String toString(int[] arr)

        //方法2:int[] grow(int[] arr),实现将数组扩大为原来2倍

public class Array {
    int number;

    public Array(int number){
        this.number = number;
    }

    public Array(){

    }

    public String toString(int[] arr){
        String a = " ";
        for (int x : arr) {
            a =a + x+"\t";
        }
        return a;
    }
    public int[] grow (int[] arr){
        int a = arr.length;
        a*=2;
        int num=0;
        int[] b = new int[a];
        for (int i = arr.length; i < b.length; i++) {
            b[i] = arr[num];
            b[num] = arr[num];
            num++;
        }
        return b;
    }
}
import java.util.Scanner;

public class Test {
    //数组长度扩大2倍
    //方法1:String toString(int[] arr)
    //方法2:int[] grow(int[] arr),实现将数组扩大为原来2倍
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int[] arrays = new int[5];
        for (int i = 0; i < arrays.length; i++) {
            arrays[i] = i+1;
        }
        Array arrayss = new Array();
        String s = arrayss.toString(arrays);
        System.out.println(s);
        String a=" ";
        for (int i = 0; i < arrayss.grow(arrays).length; i++) {
            a=a+arrayss.grow(arrays)[i]+"\t";
        }
        System.out.println(a);
    }
}

7、   //声明一个图形工具类GraphicTools,包含两个重载方法

        //方法1:double triangleArea(double base,double height)

        //根据底边和高,求三角形面积

        //方法2:double triangleArea(double a,double b,double c)

        //根据三条边,求三角形面积,根据三角形三边求面积的海伦公式

        //S=√p(p-a)(p-b)(p-c),p为半周长(周长的一半)

        //在测试类的main方法中调用

public class GraphicTools {//方法重载
    public double triangleArea(double base,double height){
        double triangleArea = base * height / 2;
        return  triangleArea;
    }

    public double triangleArea(double a,double b,double c){
        double triangleArea;
        double trianglePerimeter;
        trianglePerimeter = a + b + c;
        double p = trianglePerimeter / 2;
        triangleArea = p * (p-a) * (p-b) * (p-c);
        double sqrt = Math.sqrt(triangleArea);//开方函数
        return sqrt;
    }
}
public class Test1 {

    public static void main(String[] args) {
        GraphicTools tools = new GraphicTools();
        Scanner input = new Scanner(System.in);
        System.out.println("第一种方法:海伦公式");
        System.out.println("请输入三角形的第一条边");
        double a = input.nextDouble();
        System.out.println("请输入三角形的第二条边");
        double b = input.nextDouble();
        System.out.println("请输入三角形的第三条边");
        double c = input.nextDouble();
        double area = tools.triangleArea(a, b, c);
        System.out.println("三角形的面积是:"+area);
        System.out.println("第二种方法:底乘高/2");
        System.out.println("请输入三角形底边长度:" );
        double base = input.nextDouble();
        System.out.println("请输入三角形的高度:");
        double height = input.nextDouble();
        double area1 = tools.triangleArea(base, height);
        System.out.println("三角形的面积是:"+area1);
    }
}
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值