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.
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)
![Hoàng Anh [community,C2010G]](https://www.gravatar.com/avatar/e73c31efff649e599b1e4320b6bfb1a9.jpg?s=80&d=mm&r=g)
Hoàng Anh
2021-07-15 14:21:11
*calculator.java
import java.util.Scanner;
/*
* 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.
*/
/**
*
* @author Administrator
*/
public class calculator {
double a, b;
public calculator() {
}
public double getA() {
return a;
}
public void setA(double a) {
this.a = a;
}
public double getB() {
return b;
}
public void setB(double b) {
this.b = b;
}
public void input(){
Scanner input = new Scanner(System.in);
System.out.println("Nhập a: ");
a = input.nextDouble();
System.out.println("Nhập b: ");
b = input.nextDouble();
}
public void display(){
System.out.println("Phép tính cộng: " + (a + b));
System.out.println("Phép tính trừ : " + (a - b));
System.out.println("Phép tính nhân: " + (a * b));
System.out.println("Phép tính chia: " + (a / b));
}
}
*main.java
/*
* 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.
*/
/**
*
* @author Administrator
*/
public class main {
public static void main(String[] args) {
calculator cal = new calculator();
cal.input();
cal.display();
}
}
![GokiSoft.com [Teacher]](https://www.gravatar.com/avatar/fc6ba9324e017d540af3613b3a77dd21.jpg?s=80&d=mm&r=g)
GokiSoft.com
2021-07-14 02:43:32
#Main.java
/*
* 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 lesson05.bt986;
import java.util.Scanner;
/**
*
* @author Diep.Tran
*/
public class Main {
public static void main(String[] args) {
Calculator cal = new Calculator();
Scanner scan = new Scanner(System.in);
int choose;
// System.out.println("Tong: " + cal.cong(x, y));
// System.out.println("Hieu: " + cal.tru(x, y));
// System.out.println("Tich: " + cal.nhan(x, y));
// System.out.println("Thuong: " + cal.chia(x, y));
do {
showMenu();
choose = scan.nextInt();
if(choose == 5) break;
System.out.println("Nhap x: ");
int x = scan.nextInt();
System.out.println("Nhap y: ");
int y = scan.nextInt();
switch (choose) {
case 1:
System.out.println("Tong: " + cal.cong(x, y));
break;
case 2:
System.out.println("Hieu: " + cal.tru(x, y));
break;
case 3:
System.out.println("Tich: " + cal.nhan(x, y));
break;
case 4:
System.out.println("Thuong: " + cal.chia(x, y));
break;
// case 5:
// System.out.println("Thoat!!!");
// break;
default:
System.out.println("Nhap lai!!!");
break;
}
} while (choose != 5);
}
static void showMenu() {
System.out.println("1. Cong");
System.out.println("2. Tru");
System.out.println("3. Nhan");
System.out.println("4. Chia");
System.out.println("5. Thoat");
System.out.println("Chon: ");
}
}
#Calculator.java
/*
* 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 lesson05.bt986;
/**
*
* @author Diep.Tran
*/
public class Calculator {
public float cong(float x, float y) {
return x + y;
}
public float tru(float x, float y) {
return x - y;
}
public float nhan(float x, float y) {
return x * y;
}
public float chia(float x, float y) {
if(y == 0) {
System.out.println("Yeu cau y khac 0!!!");
return 0;
}
return x / y;
}
}
![GokiSoft.com [Teacher]](https://www.gravatar.com/avatar/fc6ba9324e017d540af3613b3a77dd21.jpg?s=80&d=mm&r=g)
GokiSoft.com
2021-07-14 02:37:47
#Main.java
/*
* 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 lesson05.bt986;
import java.util.Scanner;
/**
*
* @author Diep.Tran
*/
public class Main {
public static void main(String[] args) {
Calculator cal = new Calculator();
Scanner scan = new Scanner(System.in);
System.out.println("Nhap x: ");
int x = scan.nextInt();
System.out.println("Nhap y: ");
int y = scan.nextInt();
System.out.println("Tong: " + cal.cong(x, y));
System.out.println("Hieu: " + cal.tru(x, y));
System.out.println("Tich: " + cal.nhan(x, y));
System.out.println("Thuongw: " + cal.chia(x, y));
}
}
#Calculator.java
/*
* 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 lesson05.bt986;
/**
*
* @author Diep.Tran
*/
public class Calculator {
public float cong(float x, float y) {
return x + y;
}
public float tru(float x, float y) {
return x - y;
}
public float nhan(float x, float y) {
return x * y;
}
public float chia(float x, float y) {
if(y == 0) {
System.out.println("Yeu cau y khac 0!!!");
return 0;
}
return x / y;
}
}
![Triệu Văn Lăng [T2008A]](https://www.gravatar.com/avatar/1348e3562c6492c26f796cb1f45982a1.jpg?s=80&d=mm&r=g)
Triệu Văn Lăng
2021-03-02 14:18:47
/*
* 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 bai986;
/**
*
* @author MTLS
*/
public class Calculator {
float a, b;
public Calculator(float a1, float b1) {
a=a1;
b=b1;
}
Calculator() {
}
public float cong() {
return a + b;
}
public float tru() {
return a-b;
}
public float nhan() {
return a*b;
}
public float chia() {
return a/b;
}
}
![Triệu Văn Lăng [T2008A]](https://www.gravatar.com/avatar/1348e3562c6492c26f796cb1f45982a1.jpg?s=80&d=mm&r=g)
Triệu Văn Lăng
2021-03-02 14:18:27
/*
* 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 bai986;
import java.util.Scanner;
/**
*
* @author MTLS
*/
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
Calculator cal1 = new Calculator();
float a, b;
System.out.println("Nhap a = ");
cal1.a = scan.nextFloat();
System.out.println("Nhap b = ");
cal1.b = scan.nextFloat();
System.out.println("tong = " + cal1.cong() );
System.out.println("hieu = " + cal1.tru() );
System.out.println("tich = " + cal1.nhan() );
System.out.println("thuong = " + cal1.chia() );
}
}
![Nguyễn Hữu Hiếu [T2008A]](https://www.gravatar.com/avatar/ca2884508b617fee77f000c7d99c219d.jpg?s=80&d=mm&r=g)
Nguyễn Hữu Hiếu
2021-01-25 15:21: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 calculator;
/**
*
* @author UserName
*/
public class Pheptinh {
public static void main(String[] args) {
Caculator tinh = new Caculator();
tinh.nhap();
tinh.tong();
tinh.tich();
tinh.hieu();
tinh.thuong();
}
}
/*
* 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 calculator;
import java.util.Scanner;
/**
*
* @author UserName
*/
public class Caculator {
public int a;
public int b;
public Caculator() {
}
public void nhap() {
Scanner scan = new Scanner(System.in);
System.out.println("Nhap so thu nhat: ");
a = scan.nextInt();
System.out.println("Nhap so thu hai: ");
b = scan.nextInt();
}
public void tong() {
int c = a + b;
System.out.println("Tong 2 so = " + c);
}
public void tich() {
int c = a * b;
System.out.println("Tich 2 so = " + c);
}
public void thuong() {
int c = a / b;
System.out.println("a/b = " + c);
}
public void hieu() {
int c = a - b;
System.out.println("Hieu 2 so = " + c);
}
}
![vuong huu phu [T2008A]](https://www.gravatar.com/avatar/307a5cf29780afab49706dc8b15b86c6.jpg?s=80&d=mm&r=g)
vuong huu phu
2021-01-25 14:04:03
/*
* 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 buoi.pkg2;
import java.util.Scanner;
/**
*
* @author Admin
*/
public class Pheptinh {
public Float a, b;
int lua_chon;
public Pheptinh(float a1, float b1) {
a = a1;
b = b1;
}
Pheptinh() {
}
public void menu() {
System.out.print("Lua chon phep toan : ");
System.out.println("");
System.out.print(" 1 Phep cong");
System.out.println("");
System.out.print(" 2 Phep Tru");
System.out.println("");
System.out.print(" 3 Phep Nhan");
System.out.println("");
System.out.print(" 4 Phep Chia");
System.out.println("");
System.out.print(" Chon phep toan : ");
Scanner scan = new Scanner(System.in);
lua_chon = scan.nextInt();
}
public void cong() {
float c = a + b;
System.out.println();
System.out.println("Ket qua = "+c);
System.out.println();
}
public void tru() {
float c = a - b;
System.out.println();
System.out.println("Ket qua = "+c);
System.out.println();
}
public void nhan() {
float c = a * b;
System.out.println();
System.out.println("Ket qua = "+c);
System.out.println();
}
public void chia() {
float c = a / b;
System.out.println();
System.out.println("Ket qua = "+c);
System.out.println();
}
}
![vuong huu phu [T2008A]](https://www.gravatar.com/avatar/307a5cf29780afab49706dc8b15b86c6.jpg?s=80&d=mm&r=g)
vuong huu phu
2021-01-25 14:01:54
/*
* 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 buoi.pkg2;
import java.util.Scanner;
/**
*
* @author Admin
*/
public class Bai1 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
float a, b;
Scanner scan = new Scanner(System.in);
System.out.print("nhap so a ");
a = scan.nextFloat();
System.out.print("nhap so b ");
b = scan.nextFloat();
Pheptinh pheptinh = new Pheptinh();
pheptinh.a = a;
pheptinh.b = b;
do {
pheptinh.menu();
switch (pheptinh.lua_chon) {
case 0:
System.out.println();
System.out.println("Nhap lai");
System.out.println();
break;
case 1:
pheptinh.cong();
break;
case 2:
pheptinh.tru();
break;
case 3:
pheptinh.nhan();
break;
case 4:
pheptinh.chia();
break;
}
} while (pheptinh.lua_chon != 5 && pheptinh.lua_chon < 5);
}
}
![Đặng Trần Nhật Minh [T2008A]](https://www.gravatar.com/avatar/ee8dc5a777ad26f3a962e86c233437cf.jpg?s=80&d=mm&r=g)
Đặng Trần Nhật Minh
2021-01-25 10:01:19
package javalesson3.calculator;
import java.util.Scanner;
public class Calculator {
public int a, b;
public Calculator(int a, int b) {
this.a = a;
this.b = b;
}
public Calculator() {
}
public int sum() {
return a + b;
}
public int substract() {
return a - b;
}
public int multiply() {
return a * b;
}
public float div() {
return (float)((float)a / (float)b);
}
}
package javalesson3.calculator;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner r = new Scanner(System.in);
Calculator cal1 = new Calculator();
cal1.a = r.nextInt();
cal1.b = r.nextInt();
System.out.println("Sum = " + cal1.sum());
System.out.println("Subtraction = " + cal1.substract());
System.out.println("Multiplication = " + cal1.multiply());
System.out.println("Division = " + cal1.div());
}
}
![Nguyên Phấn Đông [T2008A]](https://www.gravatar.com/avatar/c9c4f8f79ce35b9224637b6cc5fbe5c4.jpg?s=80&d=mm&r=g)
Nguyên Phấn Đông
2021-01-25 09:10:38
/*
* 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 t2008a;
/**
*
* @author dong
*/
public class calculator {
public float x,y;
public calculator(float x1, float y1){
x = x1;
y = y1;
}
public float cong(){
return x + y;
};
public float tru(){
return x + y;
}
public float nhan(){
return x * y;
};
public float chia(){
return x / y;
}
public static void main(String[] args) {
calculator c1 = new calculator(1,2);
float c2 = c1.cong();
System.out.println("Tong 1 + 2 ="+ c2);
calculator c3 = new calculator(1,2);
float c4 = c3.tru();
System.out.println("tru 1 - 2 ="+ c4);
calculator c5 = new calculator(1,2);
float c6 = c5.nhan();
System.out.println("nhan 1 * 2 ="+ c6);
calculator c7 = new calculator(1,2);
float c8 = c7.chia();
System.out.println("chia 1 / 2 ="+ c8);
}
}