IN JAVA PLEASE HELP!
ALSO PLEASE ADD COMMENTS
Note: Could you plz go through this code and let me
know if u need any changes in this.Thank You
=================================
We have paste the input file in the project folder so
that our program can able to detect.
// fractions.txt (input file)
6/3
7/3
6/3
2/1
================================
// Fraction.java
public class Fraction {
private int numerator;
private int denominator;
public Fraction() {
this.numerator = 0;
this.denominator = 1;
}
/**
* @param numerator
* @param denominator
*/
public Fraction(int numerator, int denominator)
{
this.numerator = numerator;
if(denominator==0)
{
this.denominator
= 1;
}
else
{
this.denominator =
denominator;
}
this.reduce();
}
/**
* @return the numerator
*/
public int getNumerator() {
return numerator;
}
/**
* @param numerator the numerator to set
*/
public void setNumerator(int numerator) {
this.numerator = numerator;
}
/**
* @return the denominator
*/
public int getDenominator() {
return denominator;
}
/**
* @param denominator the denominator to set
*/
public void setDenominator(int denominator) {
this.denominator =
denominator;
}
public boolean equals(Object other)
{
Fraction o=(Fraction)other;
if(this.numerator==o.numerator
&& this.denominator==o.denominator)
{
return
true;
}
else
{
return
false;
}
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return
numerator+"/"+denominator;
}
private void reduce() {
// reduces this fraction to lowest form
int GCD = gcd(getNumerator(), getDenominator());
setNumerator(getNumerator() / GCD);
setDenominator(getDenominator() / GCD);
}
// This method will find the gcd of numerator and denominator
private int gcd(int numerator, int denominator) {
int x = numerator, y = denominator;
if (x < 0)
x = -x;
if (y > 0)
y = -y;
if (y == 0)
return x;
else
return gcd(y, x % y);
}
}
=====================================
// FractionCounter.java
public class FractionCounter {
private Fraction theFraction;
private int counter;
public FractionCounter(Fraction
theFraction) {
this.theFraction =
theFraction;
counter = 1;
}
public boolean
compareAndIncrement(Fraction theFraction) {
if
(this.theFraction.equals(theFraction)) {
counter++;
return
true;
}
return false;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return theFraction.toString() + "
has a counter of " + counter;
}
}
=========================================
// Test.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
public class Test {
public static void main(String[] args)
{
String line;
ArrayList<FractionCounter> fractions=new
ArrayList<FractionCounter>();
FractionCounter fc=null;
int num,den;
try {
Scanner sc=new
Scanner(new File("fractions.txt"));
while(sc.hasNext())
{
line=sc.nextLine();
String arr[]=line.split("/");
num=Integer.parseInt(arr[0]);
den=Integer.parseInt(arr[1]);
Fraction f=new Fraction(num,den);
if(fractions.size()==0)
{
fc=new
FractionCounter(f);
fractions.add(fc);
}
else
{
int flag=0;
for(int
i=0;i<fractions.size();i++)
{
if(fractions.get(i).compareAndIncrement(f))
{
flag=1;
}
}
if(flag==0)
{
fractions.add(new FractionCounter(f));
}
}
}
sc.close();
for(int
i=0;i<fractions.size();i++)
{
System.out.println(fractions.get(i));
}
} catch (FileNotFoundException e)
{
System.out.println(e);
}
}
}
=====================================
output:
2/1 has a counter of 3
7/3 has a counter of 1
=====================Could you plz rate me well.Thank You
IN JAVA PLEASE HELP! ALSO PLEASE ADD COMMENTS Summary Build two classes (Fraction and Fraction Counter)...
Add another public method called add to your Fraction class. This method adds another fraction to the ‘calling object’. Thus, the method will take a Fraction class object as a parameter, add this parameter fraction to the calling object (fraction), and return a Fraction object as a result. HINT: We can use cross multiplication to determine the numerator of the resultant Fraction. The denominator of the resultant Fraction is simply the multiplication of the denominators of the two other Fractions.Add...
I need help with the following Java code Consider a class Fraction of fractions. Each fraction is signed and has a numerator and a denominator that are integers. Your class should be able to add, subtract, multiply, and divide two fractions. These methods should have a fraction as a parameter and should return the result of the operation as a fraction. The class should also be able to find the reciprocal of a fraction, compare two fractions, decide whether two...
java only no c++ Write a Fraction class whose objects will represent fractions. You should provide the following class methods: Two constructors, a parameter-less constructor that assigns the value 0 to the Fraction, and a constructor that takes two parameters. The first parameter will represent the initial numerator of the Fraction, and the second parameter will represent the initial denominator of the Fraction. Arithmetic operations that add, subtract, multiply, and divide Fractions. These should be implemented as value returning methods...
C++ For this assignment you will be building on the Original Fraction class you began last week. You'll be making four major changes to the class. [15 points] Delete your set() function. Add two constructors, a default constructor that assigns the value 0 to the Fraction, and a constructor that takes two parameters. The first parameter will represent the initial numerator of the Fraction, and the second parameter will represent the initial denominator of the Fraction. Since Fractions cannot have...
java code Write a class called FractionObject that represents a fraction with an integer numerator and denominator as fields. A Fraction Object object should have the following methods: A constructor with int numerator, int denominator as parameters - Constructs a new fraction object to represent the ratio (numerator/denominator). The denominator cannot be 0, so output a message if O is passed and set the numerator and denominator to 1. A constructor with no parameters - Constructs a new FractionObject to...
C++ Fraction calculator Need help with code, cant use "using namespace std" Objectives Create and use functions. Use a custom library and namespace. Use reference variables as parameters to return values from functions. Create a robust user interface with input error checking. Use exceptions to indicate errors. Resources kishio.h kishio.cpp Assignment requirements You will need eight methods (including main). One is given to you. Their requirements are specified below: menu: The menu function takes no arguments, but returns a char...
Please use C++ and add comments to make it easier to read. Do the following: 1) Add a constructor with two parameters, one for the numerator, one for the denominator. If the parameter for the denominator is 0, set the denominator to 1 2) Add a function that overloads the < operator 3) Add a function that overloads the * operator 4) Modify the operator<< function so that if the numerator is equal to the denominator it just prints 1...
Lab 1.java only Goal: This lab will give you experience with defining and using classes and fields, and with conditionals and recursive functions. Getting Started --------------- Read the Fraction.java class into a text editor and compile it filling in the command javac -g Fraction.java. The program should compile without errors. In a shell window, run the program using "java Fraction". The program should run, although it will print fractions in a non-reduced form, like 12/20. Part I: Constructors (1 point)...
In C++ Fix any errors you had with HW5(Fraction class). Implement a function(s) to help with Fraction addition. \**************Homework 5 code*****************************/ #include<iostream> using namespace std; class Fraction { private: int wholeNumber, numerator, denominator; public: //get methods int getWholeNumber() { return wholeNumber; } int getNumerator() { return numerator; } int getDenominator() { return denominator; } Fraction()// default constructor { int w,n,d; cout<<"\nEnter whole number : "; cin>>w; cout<<"\nEnter numerator : "; cin>>n; cout<<"\nEnter denominator : "; cin>>d; while(d == 0)...
This is a C++ probelm, I am really struggling with that...... Can anyone help me?? Write a fraction class whose objects will represent fractions. For this assignment you aren't required to reduce your fractions. You should provide the following member functions: A set() operation that takes two integer arguments, a numerator and a denominator, and sets the calling object accordingly. Arithmetic operations that add, subtract, multiply, and divide fractions. These should be implemented as value returning functions that return a...