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

Java Basic- OOP - Tạo máy tính cơ bản calculator trong java

Bài 1 : 

Tạo lớp đối tượng calculator. Gồm các phương thúc sau

Cộng, Trừ, Nhân Chia -> Có 2 tham số truyền vào. Trả về kết qua tính tương tứng.

Tạo lớp Main, thực hiển sử dụng tất cả các phương thức của lớp đối tượng trên để thực hiện tính cộng, trừ, nhân, chia.



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

5

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

Nguyễn Tiến Đạt [T2008A]
Nguyễn Tiến Đạt

2021-01-25 08:51:25



/*
 * 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 lesson3;
import java.util.Scanner;

/**
 *
 * @author MyPC
 */
public class MainCalculator {
    public static void main(String[] args) {
        float x1,x2;
        Scanner scan = new Scanner(System.in);
        x1 = scan.nextFloat();
        x2 = scan.nextFloat();
        Calculator x = new Calculator();
        x.Cong(x1, x2);
        x.Tru(x1, x2);
        x.Nhan(x1, x2);
        x.Chia(x1, x2);
    }
}



/*
 * 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 lesson3;

/**
 *
 * @author MyPC
 */
public class Calculator {
    public float x1 , x2;
    public void Cong(float x1, float x2){
        System.out.println( (int)(x1+x2) ); 
    }
    public void Tru(float x1, float x2){
        System.out.println((int)(x1 - x2)); 
    }
    public void Nhan(float x1, float x2){
        System.out.println((int)(x1 * x2)); 
    }
    public void Chia(float x1, float x2){
        System.out.println(x1 / x2); 
    }
}



Minh Nghia [T1907A]
Minh Nghia

2020-03-19 06:52:44



/*
 * 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 Bt65;
import java.util.Scanner;
/**
 *
 * @author Administrator
 */
public class Main {
    public static void main(String[] args) {
        Calculator calculator = new Calculator();
        calculator.cong();
        calculator.tru();
        calculator.nhan();
        calculator.chia();
    }
}



/*
 * 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 Bt65;
import java.util.Scanner;
/**
 *
 * @author Administrator
 */
public class Calculator {
    private int a,b;
    float c;
    Scanner scan = new Scanner(System.in);
    
    public Calculator(){ 
    }
    
    public void cong(){
        System.out.println("Nhap hai so a va b:");
        System.out.println("Nhap a:");
        a  = scan.nextInt();
        System.out.println("Nhap b:");
        b = scan.nextInt();
        c = a+ b;
        System.out.println("Cong hai so = " + ( a+ b ));
        
    }
    public void tru(){
        System.out.println("Nhap hai so a va b:");
        System.out.println("Nhap a:");
        a  = scan.nextInt();
        System.out.println("Nhap b:");
        b = scan.nextInt();
        c = a - b;
        System.out.println("Tru hai so = " + ( a - b ));
        
    }
    public void nhan(){
        System.out.println("Nhap hai so a va b:");
        System.out.println("Nhap a:");
        a  = scan.nextInt();
        System.out.println("Nhap b:");
        b = scan.nextInt();
        c = a * b;
        System.out.println("Nhan hai so = " + ( a * b ));
        
    }
    public void chia(){
        System.out.println("Nhap hai so a va b:");
        System.out.println("Nhap a:");
        a  = scan.nextInt();
        System.out.println("Nhap b:");
        b = scan.nextInt();
        c = a / b;
        System.out.println("Chia hai so = " + ( a / b ));
        
    }
    
}



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

2020-03-16 13:19:58



package BT65;

public class calculator {
    float a,b;
    public float sum(float a, float b){
        float sum = a+b;
        return sum;
    }
    public float sub(float a, float b){
        float sub = a - b;
        return sub;
    }
    public float mul(float a, float b){
        float mul = a*b;
        return mul;
    }
    public float quo(float a, float b){
        float quo = a/b;
        return quo;
    }

}



package BT65;
import java.util.Scanner;
public class main {
    public static void main(String[] args) {
        calculator cal = new calculator();
        float a,b;
        Scanner sc = new Scanner(System.in);
        System.out.print("Nhập a: ");
        a= sc.nextFloat();
        sc.nextLine();
        System.out.print("Nhập b: ");
        b= sc.nextFloat();
       
        System.out.println("Summation a + b = "+cal.sum(a,b));
        System.out.println("Subtraction a - b = "+cal.sub(a,b));
        System.out.println("Multiplication a*b = "+cal.mul(a, b));
        System.out.println("quotient a/b = "+cal.quo(a, b));
    }
    
}



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

2020-03-16 13:19:56



package BT65;

public class calculator {
    float a,b;
    public float sum(float a, float b){
        float sum = a+b;
        return sum;
    }
    public float sub(float a, float b){
        float sub = a - b;
        return sub;
    }
    public float mul(float a, float b){
        float mul = a*b;
        return mul;
    }
    public float quo(float a, float b){
        float quo = a/b;
        return quo;
    }

}



package BT65;
import java.util.Scanner;
public class main {
    public static void main(String[] args) {
        calculator cal = new calculator();
        float a,b;
        Scanner sc = new Scanner(System.in);
        System.out.print("Nhập a: ");
        a= sc.nextFloat();
        sc.nextLine();
        System.out.print("Nhập b: ");
        b= sc.nextFloat();
       
        System.out.println("Summation a + b = "+cal.sum(a,b));
        System.out.println("Subtraction a - b = "+cal.sub(a,b));
        System.out.println("Multiplication a*b = "+cal.mul(a, b));
        System.out.println("quotient a/b = "+cal.quo(a, b));
    }
    
}



Lê Minh Bắc [T1907A]
Lê Minh Bắc

2020-03-16 12:34:31

Calculator:

package bt65;
import java.util.Scanner;
public class Calculator {
    public float a,b;
    
    public void insert() {
        Scanner scan = new Scanner(System.in);
        System.out.print("Nhập vào tham số thứ nhất: ");
        a = scan.nextFloat();
        scan.nextLine();
        System.out.print("Nhập vào tham số thứ hai: ");
        b = scan.nextFloat();
        scan.nextLine();
        
    }
    
    public float Addition(){
        System.out.print(a + " + " + b + " = " + (a+b));
        return a+b;
    }
    
    public float Subtraction(){  
        System.out.print(a + " - " + b + " = " + (a-b));    
        return a-b;
    }
    
    public float Multiplication(){
        System.out.print(a + " x " + b + " = " + (a*b));
        return a*b;
    }
    
    public float Division(){
        System.out.print(a + " / " + b + " = " + (a/b));
        return a/b;
    }
    
}

Main:
package bt65;
import java.util.Scanner;
public class Main {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        Calculator calculator = new Calculator();
        int choice;
        float a,b;
        do{
            System.out.println("\nMenu \n1.Addition.");
            System.out.println("2.Subtraction.");
            System.out.println("3.Multiplication");
            System.out.println("4.Division");
            System.out.println("5.Exit!!!");
            System.out.print("Nhập lựa chọn của bạn: ");
            choice = scan.nextInt();
            scan.nextLine();
            switch (choice) {
                case 1:
                    calculator.insert();
                    calculator.Addition();
                    break;
                case 2:
                    calculator.Subtraction();
                    break;
                case 3:
                    calculator.Multiplication();
                    break;
                case 4:
                    calculator.Division();
                    break;
                case 5:
                    System.out.print("EXIT!!!!");
                    break;
                default:
                    System.out.println("Lua chon cua ban chua chinh xac!!!");
                    break;
            }
        }while(choice!=5);
    }
    
}



Đăng nhập để làm bài kiểm tra

Chưa có kết quả nào trước đó