By GokiSoft.com| 19:46 15/04/2024|
Java Basic

Java Basic- OOP - Chu vi và diện hình hình tròn & hình chữ nhật

Xây dựng chương trình gồm các chức năng sau
1. Tạo lớp Circle gồm thuộc tính bán kinh và các phương thức sau
- Trả về số PI
- Nhập thông tin bán kính
- Tính chu vi hình tròn - Ko tham sô - trả về chu vi hình tròn đó
- Tính diện tích hình tròn - ko tham số - trả về diện tích hình tròn đó
2. Tạo lớp hình chữ nhật
Gồm 2 thuộc tính chiều dai và chiều rộng
Gồm các phương thức
- Nhập thông tin chiều dài và chiều rộng
- Tính chu vi
Tính diện tích
3. Xây dựng menu chương trình như sau
1. Nhập thông tin hình tròn
2. Tính chu vi hình tròn
3. Tính diện tích hình tròn
4. Nhập thông tin hcn
5. Tính chu vi hcn
6. Tính diện tích hcn
7. Thoát

Phản hồi từ học viên

5

(Dựa trên đánh giá ngày hôm nay)

Lê Xuân Dũng [JavaFree]
Lê Xuân Dũng

2020-03-21 02:05:03



package chuvidientich;

import java.util.Scanner;

public class ChuviDientich {

    public static void main(String[] args) {
//        Circle hinhtron = new Circle();
//        Rectangle hinhchunhat = new Rectangle();
//        
//        hinhtron.nhapBankinh();
//        hinhchunhat.nhap2canh();
//        
//        hinhtron.tinhChuvi();
//        hinhtron.tinhDientich();
//        
//        hinhchunhat.tinhChuvi();
//        hinhchunhat.tinhDientich();
        int n, choose;
        Scanner sc = new Scanner(System.in);
        
        Circle hinhtron = new Circle();
        Rectangle hinhchunhat = new Rectangle();
        
        do{
            System.out.println("1.Nhap thong tin hinh tron");
            System.out.println("2. Tinh chu vi hinh tron");
            System.out.println("3. Tinh dien tich hinh tron");
            System.out.println("4. Nhap thong tin hinh chu nhat");
            System.out.println("5. Tinh chu vi hinh chu nhat");
            System.out.println("6. Tinh dien tich hinh chu nhat");
            System.out.println("7. Thoat");
            choose = Integer.parseInt(sc.nextLine());
            
            switch (choose){
                case 1:
                    hinhtron.nhapBankinh();
                    break;
                case 2:
                    hinhtron.tinhChuvi();
                    break;
                case 3:
                    hinhtron.tinhDientich();
                    break;
                case 4:
                    hinhchunhat.nhap2canh();
                    break;
                case 5:
                    hinhchunhat.tinhChuvi();
                    break;
                case 6:
                    hinhchunhat.tinhDientich();
                    break;
                case 7:
                    System.out.println("Thoat!");
                    break;
                default:
                    System.out.println("Nhap sai! Hay nhap lai!");
            }
        } while (choose!=7);
            
    }
    
}


package chuvidientich;

import java.util.Scanner;

public class Circle {
    float bankinh;

    public Circle() {
    }
    
    
    public void nhapBankinh(){
        System.out.println("Nhap ban kinh hinh tron: ");
        Scanner sc = new Scanner(System.in);
        this.bankinh = sc.nextFloat();
    }
    
    public void tinhChuvi(){
        float chuvi = (float) (2*Math.PI*this.bankinh);
        System.out.println("Chu vi hinh tron co ban kinh R = "+this.bankinh+" la: "+chuvi);
    }
    
    public void tinhDientich(){
        float dientich = (float) (Math.PI*this.bankinh*this.bankinh);
        System.out.println("Dien tich hinh tron co ban kinh R = "+this.bankinh+" la: "+dientich);
    }
}


package chuvidientich;

import java.util.Scanner;

public class Rectangle {
    float chieurong;
    float chieucao;

    public Rectangle() {
    }
    
    public void nhap2canh(){
        Scanner sc = new Scanner(System.in);

        System.out.println("Nhap chieu rong hinh chu nhat: ");
        this.chieurong = sc.nextFloat();
        
        System.out.println("Nhap chieu cao hinh chu nhat: ");
        this.chieucao = sc.nextFloat();
    }
    
    public void tinhChuvi(){
        float chuvi = (float) 2*(this.chieurong+this.chieucao);
        System.out.println("Chu vi Hinh chu nhat la: "+chuvi);
    }
    
    public void tinhDientich(){
        float dientich = (float) (this.chieurong*this.chieucao);
        System.out.println("Dien tich Hinh chu nhat la: "+dientich);
    }
    
}



Minh Nghia [T1907A]
Minh Nghia

2020-03-19 06:45:28



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Bt66;
import java.util.Scanner;
/**
 *
 * @author Administrator
 */
public class Circle {
    float banKinh, C,S;
    
    public double getPI(){
        return Math.PI;
    }
    
    public void input(){
        Scanner scan = new Scanner(System.in);
        System.out.println("Nhap ban kinh:");
        banKinh = scan.nextFloat();
    }
    public float C(){
        return (float) (2 * getPI() * banKinh );
    }
    public float S(){
        return (float) ( getPI() * banKinh * banKinh);
    }
}



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Bt66;
import java.util.Scanner;
/**
 *
 * @author Administrator
 */
public class hinhChuNhat {
    float chieuDai, chieuRong,C,S;
    
    public hinhChuNhat(){
    }
    
    public void input(){
        Scanner scan = new Scanner(System.in);
        System.out.println("Nhap chieu dai la:");
        chieuDai = scan.nextFloat();
        System.out.println("Nhap chieu rong la:");
        chieuRong = scan.nextFloat();
    }
    public float C(){
       return C = (chieuDai + chieuRong)*2;
    }
    public float S(){
        return S = chieuDai * chieuRong;
    }
}



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Bt66;
import java.util.Scanner;
/**
 *
 * @author Administrator
 */
public class Main {
    public static void main(String[] args) {
        Circle cir = new Circle();
        hinhChuNhat hcn = new hinhChuNhat();
        int choose;
        Scanner scan = new Scanner(System.in);
        
        do{
            showMenu(); 
            choose = scan.nextInt();
            switch(choose){
                case 1:
                    cir.input();
                    break;
                case 2:
                    System.out.println("Chu vi hinh tron:" + cir.C());
                    break;
                case 3:
                    System.out.println("Dien tich hinh tron" + cir.S());
                    break;
                case 4:
                    hcn.input();
                    break;
                case 5 :
                    System.out.println("Chu vi hcn:" + hcn.C());
                    break;
                case 6:
                    System.out.println("Dien tich hcn:" + hcn.S());
                    break;
                case 7:
                    System.out.println("Thoat!!");
                    break;
                default:
                    System.out.println("end");
                    break;
            }
        }while( choose != 7);
    }
    
    static void showMenu(){
        System.out.println("1. Nhap thong tin hinh tron.");
        System.out.println("2. Tinh chu vi hinh tron.");
        System.out.println("3. Tinh dien tich hinh tron.");
        System.out.println("4. Nhap thong tin hcn");
        System.out.println("5. Tinh chu vi hcn.");
        System.out.println("6. Tinh dien tich hcn.");
        System.out.println("7. Thoat");
        
       
    }
}



Nguyễn Hoàng Anh [C1907L]
Nguyễn Hoàng Anh

2020-03-18 13:47:18



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package baitap4;

/**
 *
 * @author Redmibook 14
 */
public class Circle {

    public double pi = 3.14;
    public double radius;
    public void add(double radius){
        this.radius = radius;
    }
    public void perimeter() {
        double C = pi * (radius * 2);
        System.out.println("Perimeter of circle is : " + C);
    }

    public void acreage() {
        double S = pi * (radius * radius);
        System.out.println("Perimeter of circle is : " + S);
    }
}



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package baitap4;

import java.util.*;

/**
 *
 * @author Redmibook 14
 */
public class Menu {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        double radius;
        float a ;
        float b ;
        Circle circle = new Circle();
        Rectangle retangle = new Rectangle();
        while (true) {
            System.out.println("1. Nhập thông tin hình tròn");
            System.out.println("2. Tính chu vi hình tròn");
            System.out.println("3. Tính diện tích hình tròn");
            System.out.println("4. Nhập thông tin hcn");
            System.out.println("5. Tính chu vi hcn");
            System.out.println("6. Tính diện tích hcn");
            System.out.println("7. Thoát");
            String choice = input.nextLine();
            switch (choice) {
                case "1":
                    System.out.println(" Nhập bán kính hình tròn : ");
                    radius = Double.parseDouble(input.nextLine());
                    circle.add(radius);
                    break;
                case "2":
                    circle.perimeter();
                    break;
                case "3":
                    circle.acreage();
                    break;
                case "4":
                    System.out.println(" Nhập chiều dài : ");
                    a = Float.parseFloat(input.nextLine());
                    System.out.println(" Nhập chiều rộng : ");
                    b = Float.parseFloat(input.nextLine());
                    retangle.add(a,b);
                    break;
                case "5":
                    retangle.perimeter();
                    break;
                case "6":
                    retangle.acreage();
                    break;
                case "7":
                    return;

            }
        }
    }
}



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package baitap4;

/**
 *
 * @author Redmibook 14
 */
public class Rectangle {

    public float Long, wide;

    public void add(float Long, float wide) {
        this.Long = Long;
        this.wide = wide;
    }

    public void perimeter() {
        double C = (Long + wide) * 2;
        System.out.println("Perimeter of Rectangle is : " + C);
    }

    public void acreage() {
        double S =Long * wide;
        System.out.println("Acreage of Rectangle is : " + S);
    }
}



Hoàng Quang Huy [C1907L]
Hoàng Quang Huy

2020-03-18 08:31:01



//class Circle
package aptech;
import java.util.*;

public class Circle {

    public float radius;

    public float getPi() {
        return (float) Math.PI;
    }

    public void setRadius(float radius) {
        this.radius = radius;
    }

    public float getRadius() {
        return radius;
    }

    public void nhapBanKinh() {
        System.out.println("Nhập vào bán kính hình tròn: ");
         Scanner input = new Scanner(System.in);
        radius = Float.parseFloat(input.nextLine());
    }

    //Chu vi
    public void getPerimeter() {
        System.out.println("Chu vi hình tròn là: " + (getPi() * radius));
    }

    //Diện tích
    public void getArea() {
        System.out.println("Diện tích hình tròn là: " + (getPi() * getPi() * radius));
    }

}

--------------------------------------------------------------------------------
// Class Rectangle
package aptech;
import java.util.*;
public class Rectangle {

    public float length;
    public float width;

    public float getLength() {
        return length;
    }

    public void setLength(float length) {
        this.length = length;
    }

    public float getWidth() {
        return width;
    }

    public void setWidth(float width) {
        this.width = width;
    }
    //Nhập CD CR
    public void nhap() {
        Scanner input = new Scanner(System.in);
        System.out.println("Nhập chiều dài hình chữ nhật: ");
        length = Float.parseFloat(input.nextLine());
        System.out.println("Nhập chiều rộng hình chữ nhật: ");
        width = Float.parseFloat(input.nextLine());
    }

    // Chu vi
    public void getPerimeter() {
        System.out.println("Chu vi hình chữ nhật là: "+((length + width) * 2) );
    }

    //Diện tích
    public void getArea() {
        System.out.println("Diện tích hình chữ nhật là: " + (length * width));
    }
}

-----------------------------------------------------------------------------------
package aptech;

import java.util.*;

public class Main {

    public static void main(String[] args) {
        // TODO code application logic here
        Scanner input = new Scanner(System.in);
        Circle cir = new Circle();
        Rectangle rec = new Rectangle();
        boolean flag = true;
        int choice;
        float radius;
        float length, width;
        while (flag) {
            System.out.println("1. Nhập thông tin hình tròn");
            System.out.println("2. Tính chu vi hình tròn");
            System.out.println("3. Tính diện tích hình tròn");
            System.out.println("4. Nhập thông tin hcn");
            System.out.println("5. Tính chu vi hcn");
            System.out.println("6. Tính diện tích hcn");
            System.out.println("7. Thoát");
            System.out.println("Lựa chọn của bạn là: ");
            choice = Integer.parseInt(input.nextLine());
            switch (choice) {
                case 1:
                    cir.nhapBanKinh();
                    break;
                case 2:
                    cir.getPerimeter();
                    break;
                case 3:
                    cir.getArea();
                    break;
                case 4:
                    rec.nhap();
                    break;
                case 5:
                    rec.getPerimeter();
                    break;
                case 6:
                    rec.getArea();
                    break;
                case 7:
                    System.out.println("Thoát chương trình");
                    flag = false;
                    break;
                default:
                    System.out.println("Lựa chọn không hợp lệ.");
                    break;
            }
        }
    }
}



Nguyễn Hữu Đạt [C1907L]
Nguyễn Hữu Đạt

2020-03-18 05:58:26



package hinhtron;
 
import java.util.Scanner;
 
public class hinhtron{
    public static void main(String args[]) {
        Scanner input = new Scanner(System.in);
        double pi = 3.14, cv = 0, dt = 0;
        double r;
        System.out.print("Số pi có giá trị là: " + pi );
        System.out.print("\nMời nhập vào bán kính: " );
        r = input.nextDouble();
        cv = chuVi(r, pi);
        dt = dienTich(r, pi);
        System.out.print("\nChu vi hình tròn là: " + cv );
        System.out.print("\nDiện tích hình tròn là: " + dt);
    }
      public static double chuVi(double r, double pi) {
        return 2*pi*r;
      }
      public static double dienTich(double r, double pi) {
        return pi*r*r;
      }

}



package hinhchunhat;
 
import java.util.Scanner;
 
public class hinhchunhat{
    public static void main(String args[]) {
        Scanner input = new Scanner(System.in);
        double cv , dt, dai, rong;
        System.out.print("Mời nhập vào chiều dài hình chữ nhật: ");
        dai = input.nextDouble();
        System.out.print("\nMời nhập vào chiều rộng hình chữ nhật: " );
        rong = input.nextDouble();
        cv = chuVi(dai, rong);
        dt = dienTich(dai, rong);
        System.out.print("\nChu vi hình chữ nhật là: " + cv );
        System.out.print("\nDiện tích hình chữ  là: " + dt);
    }
      public static double chuVi(double dai, double rong) {
        return 2*(dai + rong);
      }
      public static double dienTich(double dai, double rong) {
        return dai*rong;
      }

}



package menu;
 
import java.util.Scanner;
 
public class Baitapvenha{
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            String choose = null;
            boolean exit = false;
            double dai = 0, rong = 0, r = 0, pi = 3.14,cvht = 0, dtht = 0, cvhcn = 0, dthcn = 0;
            showMenu();
            while (true) {
                choose = scanner.nextLine();
                switch (choose) {
                case "1":
                    System.out.println("Mời nhập vào bán kính hình tròn:");
                    r = scanner.nextDouble();
                    break;
                case "2":
                    cvht = 2*pi*r;
                    System.out.println("Chu vi hình tròn là:"+ cvht);
                    break;
                case "3":
                    dtht = pi*r*r;
                    System.out.println("Diện tích hình tròn là:"+ dtht);
                    break;
                case "4":
                    System.out.println("Mời nhập vào chiều dài hình chữ nhật:");
                    dai = scanner.nextDouble();
                    System.out.println("Mời nhập vào chiều rộng hình chữ nhật:");
                    rong = scanner.nextDouble();
                    break;
                case "5":
                    cvhcn = 2*(dai + rong);
                    System.out.println("Chu vi hình chữ nhật là:"+ cvhcn);
                    break;
                case "6":
                    dthcn = dai * rong;
                    System.out.println("Diện tích hìnhchữ nhật là:"+ dthcn);
                    break;
                case "7":
                    System.out.println("exited!");
                    exit = true;
                    break;
                default:
                    System.out.println("invalid! please choose action in below menu:");
                    break;
                }
                if (exit) {
                    break;
                }
                showMenu();
            }
        
         public static void showMenu() {
            System.out.println("-----------menu------------");
            System.out.println("1. Nhập thông tin hình tròn.");
            System.out.println("2. Tính chu vi hình tròn.");
            System.out.println("3. Tính diện tích hình tròn.");
            System.out.println("4. Nhập thông tin hình chữ nhật.");
            System.out.println("5. Tính chu vi hình chữ nhật.");
            System.out.println("6. Tính diện tích hình chữ nhật.");
            System.out.println("7. Thoát.");
            System.out.println("---------------------------");
            System.out.print("Please choose: ");
         }
    }
}



Nguyễn Hữu Đạt [C1907L]
Nguyễn Hữu Đạt

2020-03-18 05:57:54



package hinhtron;
 
import java.util.Scanner;
 
public class hinhtron{
    public static void main(String args[]) {
        Scanner input = new Scanner(System.in);
        double pi = 3.14, cv = 0, dt = 0;
        double r;
        System.out.print("Số pi có giá trị là: " + pi );
        System.out.print("\nMời nhập vào bán kính: " );
        r = input.nextDouble();
        cv = chuVi(r, pi);
        dt = dienTich(r, pi);
        System.out.print("\nChu vi hình tròn là: " + cv );
        System.out.print("\nDiện tích hình tròn là: " + dt);
    }
      public static double chuVi(double r, double pi) {
        return 2*pi*r;
      }
      public static double dienTich(double r, double pi) {
        return pi*r*r;
      }

}



package hinhchunhat;
 
import java.util.Scanner;
 
public class hinhchunhat{
    public static void main(String args[]) {
        Scanner input = new Scanner(System.in);
        double cv , dt, dai, rong;
        System.out.print("Mời nhập vào chiều dài hình chữ nhật: ");
        dai = input.nextDouble();
        System.out.print("\nMời nhập vào chiều rộng hình chữ nhật: " );
        rong = input.nextDouble();
        cv = chuVi(dai, rong);
        dt = dienTich(dai, rong);
        System.out.print("\nChu vi hình chữ nhật là: " + cv );
        System.out.print("\nDiện tích hình chữ  là: " + dt);
    }
      public static double chuVi(double dai, double rong) {
        return 2*(dai + rong);
      }
      public static double dienTich(double dai, double rong) {
        return dai*rong;
      }

}



Nguyễn Hữu Đạt [C1907L]
Nguyễn Hữu Đạt

2020-03-18 05:57:17



package hinhtron;
 
import java.util.Scanner;
 
public class hinhtron{
    public static void main(String args[]) {
        Scanner input = new Scanner(System.in);
        double pi = 3.14, cv = 0, dt = 0;
        double r;
        System.out.print("Số pi có giá trị là: " + pi );
        System.out.print("\nMời nhập vào bán kính: " );
        r = input.nextDouble();
        cv = chuVi(r, pi);
        dt = dienTich(r, pi);
        System.out.print("\nChu vi hình tròn là: " + cv );
        System.out.print("\nDiện tích hình tròn là: " + dt);
    }
      public static double chuVi(double r, double pi) {
        return 2*pi*r;
      }
      public static double dienTich(double r, double pi) {
        return pi*r*r;
      }

}



trung [C1907L]
trung

2020-03-17 15:11:09



package test;
import java.lang.Math;
import java.util.Scanner;
import javax.sound.midi.SysexMessage;
/**
 *
 * @author prdox
 */
public class Circle {
    protected float radius;

    public Circle() {
        this.radius = 0;
    }
    
    public void setRadius() {
        Scanner input = new Scanner(System.in);
        System.out.println("Nhap vao do dai hinh tron ");
        this.radius = Float.parseFloat(input.nextLine());
    }
    
    public float pi(){
        return (float) Math.PI;
    }
    
    public float findDiameter() {
        return (2 * this.pi() * this.radius);
    }
    
    public float findArea() {
        return (this.pi() * this.radius * this.radius);
    }
}

package test;

import java.util.Scanner;

/**
 *
 * @author prdox
 */
public class Rectangle {

    float x, y;

    public Rectangle() {
        this.x = 0;
        this.y = 0;
    }

    public void set(){
        Scanner input = new Scanner(System.in);
        System.out.println("Nhap vao kich thuoc hinh chu nhat");
        this.x = Float.parseFloat(input.nextLine());
        this.y = Float.parseFloat(input.nextLine());
    }
    
    public float perimeter() {
        return 2 * (this.x + this.y);
    }
    
    public float area(){
        return this.x * this.y;
    }
}

package test;

import java.util.Scanner;
import java.lang.Math;
import java.util.ArrayList;

/**
 *
 * @author prdox
 */

//Viết chương trình hiển thị thông tin cá nhân của bạn bao gồm (tên, tuổi, địa chỉ, email, sđt)
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int option;
        Circle hinhTron = new Circle();
        Rectangle hinhCn = new Rectangle();
        boolean stop = false;
        while (!stop) {
            System.out.println("1. Nhập thông tin hình tròn");
            System.out.println("2. Tính chu vi hình tròn");
            System.out.println("3. Tính diện tích hình tròn");
            System.out.println("4. Nhập thông tin hcn");
            System.out.println("5. Tính chu vi hcn");
            System.out.println("6. Tính diện tích hcn");
            System.out.println("7. Thoát");
            option = Integer.parseInt(input.nextLine());
            switch (option) {
                case 1:
                    hinhTron.setRadius();
                    break;
                case 2:
                    System.out.println(hinhTron.findDiameter());
                    break;
                case 3:
                    System.out.println(hinhTron.findArea());
                    break;
                case 4:
                    hinhCn.set();
                    break;
                case 5:
                    System.out.println(hinhCn.perimeter());
                    break;
                case 6:
                    System.out.println(hinhCn.area());
                    break;
                case 7:
                    System.out.println(hinhCn.area());
                    stop = true;
                    break;
            }
        }
    }

}



Vũ Việt Đức [C1907L]
Vũ Việt Đức

2020-03-16 16:00:42



package aptech;
import java.util.Scanner;

class Circle {
    public float radius;
    public float PI = 3.14f;

    public float getRadius() {
        return radius;
    }

    public void setRadius(float radius) {
        this.radius = radius;
    }

    public Circle(float radius) {
        this.radius = radius;
    }

    public Circle() {
    }

    public float getPI() {
        return PI;
    }
    
    public float chuVi() {
        float cv = this.PI * (this.radius * 2);
        return cv;
    }
    
    public float dienTich() {
        float dt = this.PI * this.radius * this.radius;
        return dt;
    }
    
}

class HinhChuNhat {
    public float chieuDai;
    public float chieuRong;
    
    public HinhChuNhat() {
    }

    public float getChieuDai() {
        return chieuDai;
    }

    public void setChieuDai(float chieuDai) {
        this.chieuDai = chieuDai;
    }

    public float getChieuRong() {
        return chieuRong;
    }

    public void setChieuRong(float chieuRong) {
        this.chieuRong = chieuRong;
    }
    
    public float chuVi() {
        return (2 * (this.chieuDai + this.chieuRong));
    }
    
    public float dienTich() {
        return (this.chieuDai * this.chieuRong);
    }
}

public class Homework {
    public static void main(String[] args) {
        
        Circle htron = new Circle();
        HinhChuNhat hcn = new HinhChuNhat();
        Scanner input = new Scanner(System.in);
        
        while(true) {
            System.out.println("\nVui lòng trọn: \n1.Nhập thông tin hình tròn\n2.Tính chu vi hình tròn\n3.Tính diện tích hình tròn\n4.Nhập thông tin hình chữ nhật\n5.Tích chu vi hình chữ nhật\n6.Tính diện tích hình chữ nhật\n7.Thoát");
            int chon = Integer.parseInt(input.nextLine());
            switch(chon) {
                case 1: 
                    System.out.println("Nhập thông tin hình tròn: ");
                    System.out.print("Bán kính = ");
                    htron.setRadius(Float.parseFloat(input.nextLine()));
                    break;
                case 2: 
                    System.out.println("Chu vi hình tròn là: " + htron.chuVi());
                    break;
                case 3: 
                    System.out.println("Diện tích hình tròn là: " + htron.dienTich());
                    break;
                case 4: 
                    System.out.println("Nhập thông tin hình chữ nhật: ");
                    System.out.print("Chiều dài = ");
                    hcn.setChieuDai(Float.parseFloat(input.nextLine()));
                    System.out.print("Chiều rộng = ");
                    hcn.setChieuRong(Float.parseFloat(input.nextLine()));
                    break;
                case 5: 
                    System.out.println("Chu vi hình chữ nhật là: " + hcn.chuVi());
                    break;
                case 6: 
                    System.out.println("Diện tích hình chữ nhật là: " + hcn.dienTich());
                    break;
                case 7: 
                    break;
                default:
                    System.out.println("Vui lòng chọn các số trên!");
            }
            
            if(chon == 7) {
                break;
            }
        }
        
    }
}



Trần Mạnh Dũng [T1907A]
Trần Mạnh Dũng

2020-03-16 13:19:11



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package bt66;
import java.util.Scanner;
public class HinhTron {
        private float C, S, R, Pi;
        public HinhTron(){
        }
        public HinhTron(float C, float S, float R, float Pi) {
            this.C = C;
            this.S = S;
            this.R = R;
            this.Pi = Pi;
        }
        public float C() {
           C = R * 2 * Pi;
           return C;
        }
        public float S() {
           S = R * R * Pi;
           return S;
        }
        public float getR() {
        return R;
        }
        public void seR(float R) {
        this.R = R;
        }
        public float getPi() {
        return Pi;
        }
        public void setPI(float PI) {
        this.Pi = Pi;
        } 
        public void inputR(){
            Scanner sc = new Scanner(System.in);
            System.out.print("Nhập R: ");
            R = sc.nextFloat();
            System.out.print("Nhập số Pi: ");
            Pi = sc.nextFloat();
        }
        

    }




/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package bt66;
import java.util.Scanner;
public class HinhChuNhat {
    private float ChieuDai, ChieuRong, C, S;
    public HinhChuNhat(){
    }
    public HinhChuNhat(float ChieuDai, float ChieuRong, float C, float S) {
        this.ChieuDai = ChieuDai;
        this.ChieuRong = ChieuRong;
        this.C = C;
        this.S = S;
    }
    public float getChieuDai() {
        return ChieuDai;
    }
    public void setChieuDai(float Chieudai) {
        this.ChieuDai = ChieuDai;
    }
    public float getChieuRong() {
        return ChieuRong;
    }
    public void setChieuRong(float ChieuRong) {
        this.ChieuRong = ChieuRong;
    }
    public float getC() {
        return C;
    }
    public float getS() {
        return S;
    }
    public void input() {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Nhập chiều dài: ");
        ChieuDai = scanner.nextFloat();
        System.out.print("Nhập chiều rộng: ");
        ChieuRong = scanner.nextFloat();
    }
    public void C() {
        C = (ChieuDai + ChieuRong)*2;
        System.out.println("Chu vi hình chữ nhật là: " + C);
    }
    public void S() {
        S = ChieuDai *  ChieuRong;
        System.out.println("Diện tích hình chữ nhật là: " + S);
    }
}



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package bt66;
import java.util.Scanner;
public class main {
    public static void main(String[] args) {
        HinhChuNhat hcn = new HinhChuNhat();
        HinhTron ht = new HinhTron();
        int choose;
        Scanner sc = new Scanner(System.in);
        do {
            menu();
            choose = sc.nextInt();
            switch(choose){
                case 1:
                    ht.inputR();
                    break;
                case 2: 
                    System.out.println("Chu vi hình tròn là: "+ht.C());
                    break;
                case 3:
                    System.out.println("Diện tích hình tròn: "+ht.S());
                    break;
                case 4:
                    hcn.input();
                    break;
                case 5:
                    hcn.C();
                    break;
                case 6:
                    hcn.S();
                    break;
                case 7:
                    System.out.println("Exit");
                    break;
                default:
                    System.err.println("Mời chọn lại!");
                    break;
            }

        } while (choose != 7);
    }

    public static void menu() {
        System.out.println("1: Nhập thông tin hình tròn.");
        System.out.println("2: Tính chu vi hình tròn.");
        System.out.println("3: Tính diện tích hình tròn.");
        System.out.println("4: Nhập thông tin hình chữ nhật.");
        System.out.println("5: Tính chu vi hình chữ nhật.");
        System.out.println("6: Tính diện tích hình chữ nhật");
        System.out.println("7: Thoát.");
        System.out.print("Chọn: ");
    }
}