Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
37 views

Software Lab Assignment 2

The document contains code for two Java classes: Fraction and Counter. Fraction defines a class to represent fractions with methods like reducing to lowest terms and printing. Counter defines a class to represent a counter with methods like increasing, decreasing, and resetting the count.

Uploaded by

atatsalama
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

Software Lab Assignment 2

The document contains code for two Java classes: Fraction and Counter. Fraction defines a class to represent fractions with methods like reducing to lowest terms and printing. Counter defines a class to represent a counter with methods like increasing, decreasing, and resetting the count.

Uploaded by

atatsalama
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Mohamed

Nageeb/.

20196575/CES Lab Assignment ( 2 )


1):

class Frac�on {

private int numerator;

private int denominator;

public Frac�on(int numerator, int denominator) {

this.numerator = numerator;

this.denominator = denominator;

public void setNumerator(int numerator) {

this.numerator = numerator;

public void setDenominator(int denominator) {

this.denominator = denominator;

public double getNumerator() {

return numerator;

public double getDenominator() {

return denominator;

private int calculateGCD(int a, int b) {

if (b == 0) {

return a;

} else {

return calculateGCD(b, a % b);


}

public void reduceToLowestTerms() {

int gcd = calculateGCD(numerator, denominator);

numerator /= gcd;

denominator /= gcd;

public void printFrac�on() {

System.out.println(numerator + "/" + denominator);

public class Main {

public sta�c void main(String[] args) {

Frac�on frac�on = new Frac�on(20, 60);

frac�on.reduceToLowestTerms();

frac�on.printFrac�on(); // Output: 1/3

frac�on.setNumerator(5);

frac�on.setDenominator(15);

frac�on.reduceToLowestTerms();

frac�on.printFrac�on(); // Output: 1/3

double numerator = frac�on.getNumerator();

double denominator = frac�on.getDenominator();

System.out.println("Numerator: " + numerator + ", Denominator: " + denominator);

// Output: Numerator: 1.0, Denominator: 3.0

}
2:

class Counter {

private int count;

public Counter() {

count = 0;

public void setToZero() {

count = 0;

public void increase() {

count++;

public void decrease() {

if (count > 0) {

count--;

public int getCount() {

return count;

public void printCount() {

System.out.println("Count: " + count);

@Override

public String toString() {

return "Count: " + count;

}
}

public class Main {

public sta�c void main(String[] args) {

Counter counter = new Counter();

counter.increase();

counter.increase();

counter.printCount(); // Output: Count: 2

counter.decrease();

counter.printCount(); // Output: Count: 1

counter.decrease();

counter.printCount(); // Output: Count: 0

counter.decrease();

counter.printCount(); // Output: Count: 0 (No further decrease, count remains at 0)

counter.setToZero();

counter.printCount(); // Output: Count: 0

System.out.println(counter); // Output: Count: 0

You might also like