K16T1 Nhóm 1
Bạn có muốn phản ứng với tin nhắn này? Vui lòng đăng ký diễn đàn trong một vài cú nhấp chuột hoặc đăng nhập để tiếp tục.


Hãy tưởng tượng, chúng ta đang cùng ở trên một con thuyền, tất cả đều phải cùng chèo để đưa con thuyền đến đích!
 
Trang ChínhGalleryLatest imagesTìm kiếmĐăng kýĐăng Nhập

Thông báo mới nhất, click vào để xem chi tiết: Đăng kí thành viên nhóm network!

Nhạc
Latest topics
» Assignment 02: Simple Calculating
by cambeme 18/10/2011, 23:50

» CHƯƠNG V: HỌC THUYẾT GIÁ TRỊ THẶNG DƯ - Trang 47-hết
by anhkhoamc083 5/10/2011, 08:51

» Bài tập SwingDemo
by cambeme 16/9/2011, 12:22

» ass3 cau 2
by nguyentam 29/5/2011, 10:46

» Assignment 09: Classes
by cambeme 27/5/2011, 17:57

» GROUP EXERCISE 03
by cambeme 24/5/2011, 18:23

» Bản kiểm điểm
by nhoc_vet 20/5/2011, 19:29

» Trình bày GROUP EXERCISE 02
by cambeme 20/5/2011, 00:01

» Khát là gì?
by cambeme 18/5/2011, 22:01

» Assignment 08: Methods
by cambeme 15/5/2011, 23:48

» GROUP EXERCISE 02
by cambeme 11/5/2011, 22:14

» Website nhóm 1
by cambeme 10/5/2011, 20:06

» Giao diện trang chủ
by cambeme 10/5/2011, 13:00

» Cấu hình DHCP RELAY AGENT - cấp cho 2 mạng LAN
by cambeme 9/5/2011, 23:59

» Slide 16
by cambeme 6/5/2011, 23:09

» Slide 15
by cambeme 6/5/2011, 23:08

» Slide 14
by cambeme 6/5/2011, 23:07

» Assignment 07: Control and Flow
by cambeme 6/5/2011, 22:33

» Trình bày GROUP EXERCISE 01
by cambeme 6/5/2011, 00:36

» GROUP EXERCISE 01
by cambeme 5/5/2011, 15:26

» Bai 1
by nguyentam 4/5/2011, 19:24

» cau thu 3
by cambeme 1/5/2011, 15:00

» day la bai so 2
by nguyentam 30/4/2011, 17:06

» chi tiet lam DNS
by nguyentam 30/4/2011, 16:40

» Tài liệu Windows Server 2003
by cambeme 30/4/2011, 08:40

» Phần mềm
by cambeme 29/4/2011, 12:35

» Công cụ tạo máy ảo VMware Workstation 7.1.4 Build 385546 Final
by cambeme 25/4/2011, 20:08

» Đề kiểm tra giữa kỳ Network
by cambeme 15/4/2011, 11:53

» Higher Computing Systems Networking
by cambeme 15/4/2011, 10:40

» Bài tập chương 6
by cambeme 12/4/2011, 17:41

No copy

Share | 
 

 Assignment 09: Classes

Xem chủ đề cũ hơn Xem chủ đề mới hơn Go down 
Tác giảThông điệp
cambeme
Admin
Admin
cambeme


Giới tính Giới tính : Nam
Cung : Nhân Mã
Con giáp : Mùi
Bài gửi Bài gửi : 174
Điểm Điểm : 5272
Thanks Thanks : 0
Sinh nhật Sinh nhật : 05/12/1991
Ngày gia nhập Ngày gia nhập : 25/03/2011
Tuổi Tuổi : 32
Đến từ Đến từ : Thành phố Hồ Chí minh
Nghề nghiệp/ước mơ Nghề nghiệp/ước mơ : IT
Tâm trạng Tâm trạng : Hưng phắn
Châm ngôn sống Châm ngôn sống : Không có gì quý hơn hột vịt thịt kho!
Cao nhân tắt thở vô phương trị!


Assignment 09: Classes Empty
Bài gửiTiêu đề: Assignment 09: Classes   Assignment 09: Classes I_icon_minitime27/5/2011, 17:57

Assignment 09: Classes Titleb10 27/5/2011, 17:57 » Assignment 09: Classes Assignment 09: Classes Titleb12
import java.util.Scanner;
import javax.print.attribute.standard.MediaSize.NA;

/**
* @author Anh Trinh T091020
* @comment Class cFraction A Fraction is defined by (inumerator,idenominator)
*/
@SuppressWarnings("unused")
public class cFraction {
// declare variables inumerator, idenominator
public int inumerator;
public int idenominator;

/*
* Programmed by Anh Trinh
*
* @param:inumerator=0
*
* @param:idenominator=1
*/
public cFraction() {
// TODO Auto-generated stub
// Process
inumerator = 0;
idenominator = 1;
}// end cFraction()

/*
* Programmed by Anh Trinh
*
* @param: idenominator is n
*
* @param: idenominator is 1
*/
public cFraction(int n) {
// TODO Auto-generated stub
// Process
inumerator = n;
idenominator = 1;
}// end cFraction()

/*
* Programmed by Anh Trinh
*
* @param: idenominator is p
*
* @param: idenominator is q
*/
public cFraction(int p, int q) {
// TODO Auto-generated stub
// Process
inumerator = p;
idenominator = q;
}// end cFraction()

/*
* Programmed by Anh Trinh sets the value of idenominator
*
* @param p: new value of idenominator
*
* @return nothing
*/
public void setNumberator(int p) {
// Process
inumerator = p;
}// end setNumberator

/*
* Programmed by Anh Trinh sets the value of inumerator
*
* @param q: new value of inumerator
*
* @return nothing
*/
public void setDenominator(int q) {
// Process
idenominator = q;
}// end setDenominator

/*
* Programmed by Anh Trinh irreducible the fraction
*
* @return nothing
*/
public void simplify() {
// Process
if (!isNAN()) {
// Declare variable iUCLN
int iUCLN = UCLN(inumerator, idenominator);
inumerator = inumerator / iUCLN;
idenominator = idenominator / iUCLN;
}// end if
}// end simplify

/*
* Programmed by Anh Trinh find greatest common divisor of 2 numbers
*
* @return int: greatest common divisor
*/
private static int UCLN(int a, int b) {
// Process
if (b == 0)
return a;
else if (a == 0)
return b;
else
return UCLN(b, a % b);
}// end ucln

/*
* Programmed by Anh Trinh plus current fraction with an integer
*
* @return nothing
*/
public void addInt(int n) {
// Process
inumerator = inumerator + idenominator * n;
}// end addInt

/*
* Programmed by Anh Trinh multiply current fraction with an integer
*
* @return nothing
*/
public void mulInt(int n) {
// Process
inumerator = inumerator * n;
}// end mulInt

/*
* Programmed by Anh Trinh divide current fraction with an integer
*
* @return nothing
*/
public void divInt(int n) {
// Process
idenominator = idenominator * n;
}// end divInt

/*
* Programmed by Anh Trinh get Numerator
*
* @return int: inumerator
*/
public int getNumerator() {
// Process
return inumerator;
}// end getNumerator

/*
* Programmed by Anh Trinh get Denominator
*
* @return int: iDenominator
*/
public int getDenominator() {
// Process
return idenominator;
}// end getDenominator

/*
* Programmed by Anh Trinh checks fraction <0
*
* @return boolean: true if it's Negative, false if it isn't Negative
*/
public boolean isNegative() {
// Process
return (idenominator * inumerator < 0) ? true : false;
}// end isNegative

/*
* Programmed by Anh Trinh checks fraction >0
*
* @return boolean: true if it's Positive, false if it isn't Positive
*/
public boolean isPositive() {
// Process
return (idenominator * inumerator > 0) ? true : false;
}// end isNegative

/*
* Programmed by Anh Trinh checks fraction =0
*
* @return boolean: true if it's =0, false if it isn't <>0
*/
public boolean isZero() {
// Process
return (inumerator == 0) ? true : false;
}// end isZero

/*
* Programmed by Anh Trinh checks if the fraction is an integer
*
* @return boolean: true if yes, false if no
*/
public boolean isInt() {
// Process
if (isNAN()) {
return false;
}// end isNan
if ((inumerator % idenominator) == 0) {
return true;
} else {
return false;
}
}// end isInt

/*
* Programmed by Anh Trinh checks if the fraction is irreducible
*
* @return boolean: true if it's irreducible, false if it's not irreducible
*/
public boolean isSimplest() {
// Process
if (isNAN()) {
return false;
}// end isNAN
int iUCLN = Math.abs(UCLN(inumerator, idenominator));
if (iUCLN == 1) {
return true;
} else {
return false;
}// end if else
}// end isSimplest


/*
* Programmed by Anh Trinh checks if the fraction is valid
*
* @return boolean: true if it not valid, false if it valid
*/
public boolean isNAN() {
return (idenominator == 0) ? true : false;
}// end isNAN

/*
* Programmed by Anh Trinh copy
*
* @return cFraction: f
*/
public cFraction copy() {
// Process
// Declare cFraction
cFraction f = new cFraction(inumerator, idenominator);
return f;
}// end copy

/*
* Programmed by Anh Trinh absolute
*
* @return cFraction: pq
*/
public cFraction abs() {
// Declare fraction
cFraction fraction = copy();
// Process
fraction.inumerator = Math.abs(inumerator);
fraction.idenominator = Math.abs(idenominator);
return fraction;
}// end abs

/*
* Programmed by Anh Trinh plus current fraction with fraction pq
*
* @return cFraction: pq
*/
public cFraction add(cFraction pq) {
// Process
pq.inumerator = inumerator * pq.idenominator + idenominator
* pq.inumerator;
pq.idenominator = idenominator * pq.idenominator;
return pq;
}// end add

/*
* Programmed by Anh Trinh plus current fraction with n
*
* @return cFraction: f
*/
public cFraction add(int n) {
// Process
cFraction f = copy();
f.addInt(n);
return f;
}// end add

/*
* Programmed by Anh Trinh subtract current fraction from pq
*
* @return cFraction: pq
*/
public cFraction sub(cFraction pq) {
// Process
pq.inumerator = inumerator * pq.idenominator - pq.inumerator
* idenominator;
pq.idenominator = idenominator * pq.idenominator;
return pq;
}// end sub

/*
* Programmed by Anh Trinh subtract current fraction from n
*
* @return cFraction: f
*/
public cFraction sub(int n) {
// Process
cFraction f = copy();
f.inumerator = f.inumerator - n * f.idenominator;
return f;
}// end sub

/*
* Programmed by Anh Trinh multiply current fraction with pq
*
* @return cFraction: pq
*/
public cFraction mul(cFraction pq) {
// Process
pq.inumerator = inumerator * pq.inumerator;
pq.idenominator = idenominator * pq.idenominator;
return pq;
}// end mul

/*
* Programmed by Anh Trinh multiply current fraction with n
*
* @return cFraction: f
*/
public cFraction mul(int n) {
// Process
cFraction f = copy();
f.mulInt(n);
return f;
}// end mul

/*
* Programmed by Anh Trinh divide current fraction with pq
*
* @return cFraction: pq
*/
public cFraction div(cFraction pq) {
// declare variable iVar
int iVar = pq.inumerator;
pq.inumerator = inumerator * pq.idenominator;
pq.idenominator = idenominator * iVar;
return pq;
}// end div

/*
* Programmed by Anh Trinh divide current fraction with n
*
* @return cFraction: f
*/
public cFraction div(int n) {
cFraction f = copy();
f.divInt(n);
return f;
}// end div

/*
* Programmed by Anh Trinh check if pq is the same as current fraction
*
* @return boolean: True if they'r same, False if not
*/
public boolean equals(cFraction pq) {
if (isNAN() || pq.isNAN()) {
return false;
}// end if
return (inumerator * pq.idenominator == idenominator * pq.inumerator) ? true
: false;
}// end equals

/*
* Programmed by Anh Trinh check if pq is the same as current fraction
*
* @return boolean: True if they'r same, False if not
*/
public int compareTo(cFraction pq) {
if (isNAN() || pq.isNAN()) {
return -1;
}// end if
return (inumerator * pq.idenominator >= idenominator * pq.inumerator) ? 0
: -1;
}// end compareTo

/*
* Programmed by Anh Trinh return current fraction to string
*
* @return String: current fraction
*/
public String toString() {
if (isInt()) {
return "" + (inumerator / idenominator);
}// end isInt
if (isNAN()) {
return "NAN";
}// end NAN

// Declare cFraction
cFraction f = copy();
if (isNegative()) {
f = f.abs();
return "-" + f.inumerator + "/" + f.idenominator;
}// end <0
if (isPositive()) {
f = f.abs();
return f.inumerator + "/" + f.idenominator;
}// end >0
return "";
}// end toString

/*
* Programmed by Anh Trinh return current fraction to double
*
* @return double: current fraction
*/
public double toDouble() {
if (isNAN()) {
return Double.NaN;
}// end NAN
return 1.0 * inumerator / idenominator;
}// end toDouble

public static void main(String[] args) {
// TODO Auto-generated method stub
// the scanner
Scanner input = new Scanner(System.in);
// declare the variable
int x1, x2, y1, y2;
// input x1 from keyboard
System.out.print("Nhap tu so x1: ");
x1 = input.nextInt();
// input y1 from keyboard
System.out.print("Nhap mau so y1: ");
y1 = input.nextInt();
// create a new cFraction array
cFraction A = new cFraction(x1, y1);
// print out the numerator
System.out.println("Gia tri tu so la:" + A.getNumerator());
// print out the denominator
System.out.println("Gia tri mau so la:" + A.getDenominator());
// print out the result
System.out.println("Kiem tra phan so co be hon 0 hay khong:"
+ A.isNegative());
// print out the result
System.out.println("Kiem tra phan so co lon hon 0 hay khong:"
+ A.isPositive());
// print out the result
System.out.println("Kiem tra co la phan so toi gian hay khong:"
+ A.isSimplest());
// print out the result
System.out.println("Kiem tra co phai la so nguyen hay khong:"
+ A.isInt());
// print out the result
System.out
.println("Kiem tra phan so co bang 0 hay khong:" + A.isZero());
// print out the result
System.out.println("kiem tra mau so co bang 0 hay khong:" + A.isNAN());
// print out the absolutely value
System.out.println("Tri tuyet doi la:" + A.abs());
System.out.println("COPY:" + A.copy());
// input x2 from keyboard
System.out.println("Nhap tu so x2: ");
x2 = input.nextInt();
// input y2 from keyboard
System.out.println("Nhap mau so y2: ");
y2 = input.nextInt();
// create a new cFraction array
cFraction y = new cFraction(x2, y2);
// print out sum of 2 fractions
cFraction x = A.add(y);
System.out.println("ket qua cong 2 phan so:" + x.toString());
// print out difference of 2 fractions
System.out.println("ket qua tru 2 phan so:" + A.sub(y));
// print out product of 2 fractions
System.out.println("ket qua nhan 2 phan so:" + A.mul(y));
// print out quotient of 2 fractions
System.out.println("ket qua chia 2 phan so:" + A.div(y));
// print out the result
System.out
.println("Kiem tra phan so hien tai co bang phan so ban dau hay khong:"
+ A.equals(y));
// print out the result
System.out.println("So sanh phan so hien tai voi phan so ban dau:"
+ A.compareTo(y));
// print out the result as fraction
System.out.println("Tra ra chuoi theo yeu cau:" + A.toString());
// print out the result as real number
System.out.println("Tra ra so thuc:" + A.toDouble());
}// end main
}



Assignment 09: Classes Border11 Assignment 09: Classes Border14
Về Đầu Trang Go down
https://nhom1.forum-viet.com
 

Assignment 09: Classes

Xem chủ đề cũ hơn Xem chủ đề mới hơn Về Đầu Trang 

 Similar topics

-
» Assignment 05 – Imazing
» Assignment 08: Methods
» Assignment 01 – HelloWorld
» Assignment 04 – Simple Loop
» Assignment 06 – Loop And Array
Trang 1 trong tổng số 1 trang

Permissions in this forum:Bạn không có quyền trả lời bài viết
K16T1 Nhóm 1 :: Các môn học :: Năm 1 :: Java-
Chuyển đến 
Đầu trang
Giữa trang
Cuối trang
Free forum | ©phpBB | Free forum support | Báo cáo lạm dụng | Thảo luận mới nhất