Yam Code
Sign up
Login
New paste
Home
Trending
Archive
English
English
Tiếng Việt
भारत
Sign up
Login
New Paste
Browse
package Buoi5.bai2; //Bài 2: Một phân số gồm có 2 thuộc tính là tử số (numerator) và mẫu số (denominator). Hãy thiết kế class Phân số (Rational) để thực hiện các chức năng sau: // - Tối giản phân số (reduce) // - Nghịch đảo phân số (reciprocal) // - Cộng 2 phân số (add) // - Trừ 2 phân số (subtract) // - Nhân 2 phân số (multiply) // - Chia 2 phân số (divide) // - So sánh 2 phân số, sử dụng sai số 0.0001(tolerance) để so sánh. public class Rational { private int Tuso; private int Mauso; private int Tuso2; private int Mauso2; public static int UCLN(int a, int b) { return (b==0) ? a : UCLN(b,a%b); } public void ToiGianPhanSo() { int uc = UCLN(this.Tuso,this.Mauso); System.out.println(this.Tuso/uc + "/" + this.Mauso/uc); } public void Reciprocal() { System.out.println(this.Mauso + "/" + this.Tuso); } public void Add() { int tu= this.Tuso*this.Mauso2 + this.Tuso2*this.Mauso; int mau = this.Mauso*this.Mauso2; System.out.println( this.Tuso + "/"+ this.Mauso + " + " + this.Tuso2 + "/" + this.Mauso2 + " = "+ tu + "/" + mau); } public void Subtract() { int tu= this.Tuso*this.Mauso2 - this.Tuso2*this.Mauso; int mau = this.Mauso*this.Mauso2; System.out.println( this.Tuso + "/"+ this.Mauso + " - " + this.Tuso2 + "/" + this.Mauso2 + " = "+ tu + "/" + mau); } public void Multiply() { int tu= this.Tuso*this.Tuso2; int mau = this.Mauso*this.Mauso2; System.out.println( this.Tuso + "/"+ this.Mauso + " X " + this.Tuso2 + "/" + this.Mauso2 + " = "+ tu + "/" + mau); } public void Divine() { int tu= this.Tuso*this.Mauso2; int mau = this.Mauso*this.Tuso2; System.out.println( this.Tuso + "/"+ this.Mauso + " : " + this.Tuso2 + "/" + this.Mauso2 + " = "+ tu + "/" + mau); } public void Comparisons() { float sothunhat = (float) this.Tuso/this.Mauso; float sothuhai = (float) this.Tuso2/this.Mauso2; float hieu = sothunhat-sothuhai; System.out.println("Phan so thu nhat "); if(hieu > 0 ) { System.out.println("Lon"); } } // construction public Rational(int tuso, int mauso) { Tuso = tuso; Mauso = mauso; } // get and set public int getTuso() { return Tuso; } public void setTuso(int tuso) { Tuso = tuso; } public int getMauso() { return Mauso; } public void setMauso(int mauso) { Mauso = mauso; } }
Paste Settings
Paste Title :
[Optional]
Paste Folder :
[Optional]
Select
Syntax Highlighting :
[Optional]
Select
Markup
CSS
JavaScript
Bash
C
C#
C++
Java
JSON
Lua
Plaintext
C-like
ABAP
ActionScript
Ada
Apache Configuration
APL
AppleScript
Arduino
ARFF
AsciiDoc
6502 Assembly
ASP.NET (C#)
AutoHotKey
AutoIt
Basic
Batch
Bison
Brainfuck
Bro
CoffeeScript
Clojure
Crystal
Content-Security-Policy
CSS Extras
D
Dart
Diff
Django/Jinja2
Docker
Eiffel
Elixir
Elm
ERB
Erlang
F#
Flow
Fortran
GEDCOM
Gherkin
Git
GLSL
GameMaker Language
Go
GraphQL
Groovy
Haml
Handlebars
Haskell
Haxe
HTTP
HTTP Public-Key-Pins
HTTP Strict-Transport-Security
IchigoJam
Icon
Inform 7
INI
IO
J
Jolie
Julia
Keyman
Kotlin
LaTeX
Less
Liquid
Lisp
LiveScript
LOLCODE
Makefile
Markdown
Markup templating
MATLAB
MEL
Mizar
Monkey
N4JS
NASM
nginx
Nim
Nix
NSIS
Objective-C
OCaml
OpenCL
Oz
PARI/GP
Parser
Pascal
Perl
PHP
PHP Extras
PL/SQL
PowerShell
Processing
Prolog
.properties
Protocol Buffers
Pug
Puppet
Pure
Python
Q (kdb+ database)
Qore
R
React JSX
React TSX
Ren'py
Reason
reST (reStructuredText)
Rip
Roboconf
Ruby
Rust
SAS
Sass (Sass)
Sass (Scss)
Scala
Scheme
Smalltalk
Smarty
SQL
Soy (Closure Template)
Stylus
Swift
TAP
Tcl
Textile
Template Toolkit 2
Twig
TypeScript
VB.Net
Velocity
Verilog
VHDL
vim
Visual Basic
WebAssembly
Wiki markup
Xeora
Xojo (REALbasic)
XQuery
YAML
HTML
Paste Expiration :
[Optional]
Never
Self Destroy
10 Minutes
1 Hour
1 Day
1 Week
2 Weeks
1 Month
6 Months
1 Year
Paste Status :
[Optional]
Public
Unlisted
Private (members only)
Password :
[Optional]
Description:
[Optional]
Tags:
[Optional]
Encrypt Paste
(
?
)
Create New Paste
You are currently not logged in, this means you can not edit or delete anything you paste.
Sign Up
or
Login
Site Languages
×
English
Tiếng Việt
भारत