A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

微笑c位

初级黑马

  • 黑马币:23

  • 帖子:8

  • 精华:0

© 微笑c位 初级黑马   /  2019-3-12 11:44  /  851 人查看  /  1 人回复  /   1 人收藏 转载请遵从CC协议 禁止商业使用本文

来到黑马已经10天了,代码也就会敲一丢丢了,演示一波:
  1 首先 复制粘贴 就完事了
定义一个圆形Circle类。
       属性:
       r:半径
         构造方法:
             无参构造方法
             满参构造方法
         成员方法:
            get/set方法
            showArea方法:打印圆形面积
            showPerimeter方法:打印圆形周长
        定义测试类,创建Circle对象,并测试
          > 面向周长公式:2 * 3.14*  半径
          >
          > 圆形面积公式:3.14* 半径^2


package com.itcast.day03zuoye;

public class Circle {
    private int r;

    public Circle() { }
    public  Circle(int r){
        this.r=r;
    }

    public int getR() {
        return r;
    }

    public void setR(int r) {
        this.r = r;
    }
    public void showArea(){

        System.out.println("圆形面积:"+Math.PI*r*r);
    }
    public void showPerimeter(){
        System.out.println("面向周长:"+2*Math.PI*r);
    }
}


package com.itcast.day03zuoye;

public class CircleDemo {
    public static void main(String[] args) {
        Circle circle=new Circle(4);
        circle.showArea();
        circle.showPerimeter();
    }
}


手机类Phone
        属性:
            品牌brand
            价格price
        行为:
            打电话call()
            发短信sendMessage()
            玩游戏playGame()
   
        要求:
            1.按照以上要求定义类,属性要私有,生成空参、有参构造,setter和getter方法
            2.定义测试类,在main方法中创建该类的对象并给属性赋值(演示两种方法:setter方法和构造方法)
            3.调用三个成员方法,打印格式如下:
                正在使用价格为998元的小米品牌的手机打电话....
                正在使用价格为998元的小米品牌的手机发短信....
                正在使用价格为998元的小米品牌的手机玩游戏....


package com.itcast.day03zuoye;

public class Phone {
private String brand;
    private int price;





    public Phone() {}
    public Phone(String brand,int price){
        this.brand=brand;
        this.price=price;
    }

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }
    public void call(){
        System.out.println("正在使用价格为"+price+"的"+brand+"品牌的手机打电话");
    }
    public void sendMessage(){
        System.out.println("正在使用价格为"+price+"的"+brand+"品牌的手机发短信");
    }
    public void playGame(){
        System.out.println("正在使用价格为"+price+"的"+brand+"品牌的手机玩游戏");
    }
}


package com.itcast.day03zuoye;
//手机测试类
public class PhoneDemo {
    public static void main(String[] args) {
        Phone c=new Phone("小米",888);
        c.call();
        c.sendMessage();
        c.playGame();
        System.out.println("====================================");
        Phone a=new Phone();
        a.setBrand("小米");
        a.setPrice(888);
        c.call();
        c.sendMessage();
        c.playGame();
    }
}


  给出数组 {6,66,43,45,38,64,21,99,88}
       1.求出数组中奇数的和
       2.求出数组中偶数的和
       3.把奇数的平均数和偶数的平均数组成一个数组进行遍历

import java.util.Arrays;

public class zuoye2 {
   
    public static void main(String[] args) {
        int[] arr = {6, 66, 43, 45, 38, 64, 21, 99, 88};
        int sum = jishu(arr);
        System.out.println("数组中奇数的和" + sum);
        int sum1 = oushu(arr);
        System.out.println("数组中偶数的和" + sum1);
        int[] brr = {sum, sum1};
        System.out.print(Arrays.toString(brr));
    }

    public static int jishu(int[] arr) {
        int sum = 0;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] % 2 == 1) {
                sum += arr[i];
            }
        }
        return sum;
    }

    public static int oushu(int[] arr) {
        int sum1 = 0;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] % 2 == 0) {
                sum1 += arr[i];
            }
        }
        return sum1;
    }
}

1 个回复

正序浏览
正在学这个 正好用上
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马