Class Diagram Worksheet C++ 3) Class name is Fraction has the attributes: numera
ID: 3918426 • Letter: C
Question
Class Diagram Worksheet C++
3) Class name is Fraction has the attributes: numerator (an integer) and denominator (an integer). The methods should include a default constructor with default values of 1 for each attribute, a set function for each attribute, a setAll function that sets values for both attributes, a get function for each attribute, a decimalValue function that returns the quotient as a decimal, a ratioValue that returns the ratio (example: 3/7).
Create a class diagram
Create the class specification/interface
Create a class implementation
Constructor sets all of the attributes to 0
A set function for each attribute
The set function for denominator must validate parameter because it can not be 0. If 0, then set denominator to 1
A setAll function that accepts two integer and assigns them to the attributes
A get function for each attribute that returns the value of the attribute
A decimalValue function that returns the quotient as a decimal
A ratio function that returns the reduced ratio of numerator/denominator
Suppose the ratio is 6/14, the function will return the reduced equivalent of 3/7 as a string
Need to use a greatest common divisor algorithm
Add the class interface and implementation to a test driver
Class Diagram Worksheet C++
4) Class name is StudentModel has the attributes: a list of Student objects, size of the Student list (an integer), active status (a Boolean). The methods should include a constructor that loads the student data into the Student list from a data file, an add function that adds a student to the end of the list, a remove function that sets active status to false, a findPosition function that accepts a position in the list and returns a student’s first and last name at that given position (0 to list size minus 1), a findName function that accepts a last name and displays the full name and list position all of the students with the last name, a listSize function that returns the size of the list, a sort function that organizes the list by either student last name or average score.
Create a class diagram
Create the class specification/interface
Create a class implementation
Constructor loads the Student list from a data file
An add function that adds a student to the end of the list
A remove function that accepts student last name, calls the findName function and prompts the user to select position from the list, then sets active status to false if and only if the student name is found.
A findPosition function that accepts a position in the list and returns a student’s first and last name at that given position (0 to list size minus 1
A findName function that accepts a last name and displays the full name and list position all of the students with the last name
A listSize function that returns the size of the list,
A sort function that organizes the list by either student last name or average score.
Add the class interface and implementation to a test driver
Explanation / Answer
ANSWER:
THE BELOW IS THE CODE FOR THE GIVEN DATA;
-->CODE FOR 3:
//Fraction.java
public class Fraction
{
private int numerator;
private int denominator;
public Fraction()
{
numerator = 1;
denominator = 1;
}
public Fraction(int num , int den)
{
setAll(num,den);
}
public void setAll(int num , int den)
{
numerator = num;
setDenominator(den);
}
public void setNumerator(int num)
{
numerator = num;
}
public void setDenominator(int den)
{
if(denominator==0)
this.denominator=1;
else
denominator = den;
}
public int getNumerator()
{
return numerator;
}
public int getDenominator()
{
return denominator;
}
public double decimalValue()
{
return (double)(numerator)/denominator;
}
public String ratioValue()
{
int commondivisor=gcd(numerator, denominator);
numerator=numerator/commondivisor;
denominator=denominator/commondivisor;
return new String(numerator+"/"+denominator);
}
private int gcd(int n, int d)
{
int remainder;
while (d != 0)
{
remainder = n % d;
n = d;
d = remainder;
}
return n;
}
public String toString() {
return numerator+"/"+denominator;
}
}
//FractionDriver.java
public class FractionDriver
{
public static void main(String[] args)
{
Fraction fraction1=new Fraction();
System.out.println("Default fraction");
System.out.println(fraction1);
fraction1.setNumerator(10);
fraction1.setDenominator(5);
System.out.println("setting fraction values");
System.out.println(fraction1);
fraction1.setAll(6 , 14);
System.out.println("Using setAll metod");
System.out.println(fraction1);
System.out.println("calling ratioValue method");
System.out.println(fraction1.ratioValue());
System.out.println("Calling decimalValue method");
System.out.println(fraction1.decimalValue());
}
}
OUTPUT:
Default fraction
1/1
setting fraction values
10/5
Using setAll metod
6/14
calling ratioValue method
3/7
Calling decimalValue method
0.428571428571487
Hence the required out put is obtained