J Unit Testing for Multiplication in Java - java

public static int multiply(int a, int b) {
int product = a * b;
return product;
}
I am trying to write a J Unit test for this code. Right now it passes, but I am not fully sure if I have it correct. I am also not fully sure if the code is correct to begin with. The code is suppose to take two rational numbers as parameters and return a Rational number as their product.
#Test
public void multiplyTest() {
int product = Rational.multiply(5/7,2/3);
assertEquals(product, Rational.multiply(5/7, 2/3));
}
Update
Here is my Rational class with my actual code:
public class Rational {
private int num;
private int den;
public Rational(int numIn, int denIn) {
num = numIn;
den = denIn;
}
public int getNum() {
return num;
}
public int getDen() {
return den;
}
public String toString() {
return num + "/" + den;
}
public String reciprocal() {
return den + "/" + num;
}
public static int multiply(int a, int b) {
int product = a * b;
return product;
}
public int divide(int a) {
int number = num / den;
return number / a;
}
public int add(int number) {
int sum = ((this.num * den) + (num * this.den)) / (this.den * den);
return sum;
}
}

The solution is not correct nor is the test.
Your method takes as inputs two integers and returns an integer.
You need to create a Rational class with nominator and denominator fields.
Use it as the type of arguments and return type.
Also you need to tell the result to the test which is 10/21 and the test will determine if the method under test can get the correct result . The given junit uses the same method to calculate the same thing twice and then verifies that the results are the same. They are of course the same but this proves nothing.
Update
Based on your update I provide an updated version of your Rational class.
Similar changes can be done to the other methods. Notice that it would be better for reciprocal to return a rational so that the programmer can also use it.
You can still print it by writting rational.reciprocal() as toString will be automatically called for example in System.out.println(rational.reciprocal());
public class Rational {
private final int num;
private final int den;
public Rational(int numIn, int denIn) {
num = numIn;
den = denIn;
}
public int getNum() {
return num;
}
public int getDen() {
return den;
}
public String toString() {
return num + "/" + den;
}
public Rational reciprocal() {
return new Rational(den,num);
}
public static Rational multiply(Rational a, Rational b) {
return new Rational(a.num * b.num , a.den * b.den );
}
public Rational divide(int a) {
return new Rational(this.num,a*this.den);
}
}

Your code just calls the code under test twice. That would test that multiply is idempotent (at least when called twice). It does not test that it actually multiplies its parameters. If it just added them and returned the sum, your test would not notice (i.e., fail).
Your test is also quite complicated; why the division in the code?
Do something like this instead:
#Test
public void multiplyInts() {
assertEquals(Integer.valueOf(35), Rational.multiply(5, 7));
}
Repeat with real or long, if that is important to your code under test.

Related

How to aggregate methods from another class?

I have a class Fraction with the arithmetic operations for fractions. Here is an abstract of my class Fraction. (I've included only method of addition.)
package com.company;
import java.util.Scanner;
public class Fraction {
private int num; // numerator
private int denom; // denominator
public Fraction() {
super();
}
public Fraction(int num, int denom) {
super();
this.num = num;
this.denom = denom;
if (denom == 0) {
this.denom = 1;
}
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public int getDenom() {
return denom;
}
public void setDenom(int denom) {
if (denom > 0) {
this.denom = denom;
}
}
public void inputFraction() {
Scanner innum = new Scanner(System.in);
System.out.println("Input numerator: ");
num = innum.nextInt();
Scanner indenom = new Scanner(System.in);
System.out.println("Input denominator: ");
denom = indenom.nextInt();
}
public String toString() {
return num + "/" + denom;
}
// addition
public Fraction add(Fraction f2) {
int num2 = f2.getNum();
int denom2 = f2.getDenom();
int num3 = (num * denom2) + (num2 * denom);
int denom3 = denom * denom2;
Fraction f3 = new Fraction(num3, denom3);
f3.simplifyFraction();
return f3;
}
}
Now my second task is to make a class Calculator, which aggregates two instances of class Fraction as its attributes and create a complete set of arithmetic operations using instances of the class Fraction as operands. So, if I am correct, I basically have to use those methods from the class Fraction in my Calculator. I've attempted to do that but I do not get any output when I call for method add (from class Calculator) in main().
Here is an abstract of my Calculator class. (I've included only method of addition to give the general idea.)
package com.company;
public class Calculator {
private Fraction f1 = new Fraction();
private Fraction f2 = new Fraction();
private Fraction f;
public Calculator() {
super();
}
public Calculator(Fraction f) {
this.f = f;
}
public void input() {
f1.inputFraction();
f2.inputFraction();
}
public void view() {
f1.toString();
System.out.println("Fraction = " + f1.toString());
f2.toString();
System.out.println("Fraction = " + f2.toString());
}
public Calculator add() {
Calculator f = new Calculator(f1.add(f2));
return f;
}
}
And part of my main():
Calculator m = new Calculator();
m.input();
m.view();
System.out.println("Sum = " + m.add());
I'm assuming there are multiple places where I have gone wrong, so I'd be grateful for some advice.
Your add method is the problem here. It is returning a Calculator object and println calls that object's toString method so the toString method for Calculator is being called. The add method should not return a new Calculator but instead a new Fraction that represents your result. Then the code will print the toString method in your Fraction class which is what you want to display.
public class Calculator {
.
.
.
public Fraction add() {
return f1.add(f2);
}
}

Issue with Scope and Access

Using Java and trying to solve the following problem, my code keeps giving this error:
"TesterClass.java:41: error: incompatible types: int cannot be converted to String
return numerator;"
The code is supposed to print out 0 and and a 1.
I have changed about everything around, I know it is a scope and access issue, just not sure where it is.
public class TesterClass
{
public static void main(String[] args)
{
Fraction f1 = new Fraction();
Fraction f2 = new Fraction(1,2);
System.out.println(f1);
System.out.println(f2.numerator / f2.denominator); //class Scope
}
}
/** Class Fraction */
class Fraction
{
// instance variables
private int numerator;
private int denominator;
// constructor: set instance variables to default values
public Fraction()
{
int d = 1;
numerator = d;
denominator = d;
}
// constructor
public Fraction(int initNumerator, int initDenominator)
{
numerator = initNumerator;
denominator = initDenominator;
}
public String toString()
{
if (denominator == 1)
return numerator;
return numerator + "/" + denominator;
}
public int getNum()
{
return numerator;
}
public int getDen()
{
return denominator;
}
}
Should print out
1
0
return numerator; in itself cannot work because the method is supposed to return a String but numeratoris of type int.
If you want to keep the distiction using if (denominator == 1) you need to manually convert numerator to a String.
You could for example do that using
return String.valueOf(numerator)
(thanks #Antonio)
public class TesterClass
{
public static void main(String[] args)
{
Fraction f1 = new Fraction();
Fraction f2 = new Fraction(1,2);
System.out.println(f1.getNum());
System.out.println(f1);
System.out.println(f2);
}
}
/** Class Fraction */
class Fraction
{
// instance variables
private int numerator;
private int denominator;
// constructor: set instance variables to default values
public Fraction()
{
int d = 1;
int n = 1;
numerator = n;
denominator = d;
}
// constructor: set instance variables to init parameters
public Fraction(int initNumerator, int initDenominator)
{
numerator = initNumerator;
denominator = initDenominator;
}
public String toString()
{
if (denominator == 1)
return String.valueOf(numerator);
return String.valueOf(numerator) + "/" + String.valueOf(denominator);
}
public int getNum()
{
return numerator;
}
public int getDen()
{
return denominator;
}
}
You also trying access private fields numerator and denominator from TesterClass class.
To solve your error you may do:
public String toString() {
if (denominator == 1)
return numerator+"";
return numerator + "/" + denominator;
}

Doing a course on Code HS, errors say "Bad operator types for binary operator" for lines with the public void. Not sure what I'm doing wrong

EXERCISE DIRECTIONS
In this exercise, you must take your Fraction class
from earlier and extend it by adding a few handy methods.
public void add(Fraction other)
public void subtract(Fraction other)
public void multiply(Fraction other)
public int getNumerator();
public int getDenominator();
public void setNumerator(int x);
public void setDenominator(int x);
public String toString();
Use the FractionTester file to test as you go along.
Note that
public void add(Fraction other)
public void subtract(Fraction other)
public void multiply(Fraction other)
are void methods. They do not return anything. These methods should not create a new Fraction and return it.
Instead, these methods should modify the instance variables to be added, subtracted, or multiplied by the Fraction other.
For example, the following code:
Fraction first = new Fraction(1, 2);
Fraction second = new Fraction(1, 3);
System.out.println();
System.out.println("BEFORE:");
System.out.println("first: " + first);
System.out.println("second: " + second);
first.multiply(second);
System.out.println("AFTER:");
System.out.println("first: " + first);
System.out.println("second: " + second);
Should print out:
BEFORE:
first: 1 / 2
second: 1 / 3
AFTER:
first: 1 / 6
second: 1 / 3
The Fraction first was modified by being multiplied by the Fraction second. first was affected, second was not. 1/2 became 1/6 because it was multiplied by 1/3.
This is my code:
public class Fraction
{
// Create your instance variables and constructor here
//Instance variables
private int num;
private int den;
//Constructor
public Fraction(int nume, int dene)
{
num = nume;
den = dene;
}
public void add(Fraction other)
{
Fraction a = num/den + other;
}
public void subtract(Fraction other)
{
Fraction b = num/den - other;
}
public void multiply(Fraction other)
{
Fraction c = num/den * other;
}
public String toString()
{
return "";
}
}
You can't multiply an int (e.g. den or num) directly by a Fraction object. You need to dereference the passed fraction argument and then update the den and num components of the calling instance.
This
public void multiply(Fraction other) {
Fraction c = num/den * other;
}
Needs to be replaced with this.
public void multiply(Fraction other) {
num = num * other.num;
den = den * other.den;
}
When you add or subtract, you need to find common denominators.

A code that will accept two integers and return the larger of the two integers

this is my first coding class ever and i'm very new to all this please. I'm trying to create a code that will accept two numbers and return the larger of the two numbers. For e.g if the function is given da integers 7 and 12 the function will return da integer 12.
This is the code i've written so far but it's far from being correct.
public class Return
{
public static void main(String[] args)
{
public static int max("int num1, int num2,");
int result;
if (num1 > num2)
result = num1;
else
result = num2;
return result;
}
}
Java do not have nested methods. You write method inside method. Move your method outside and there are some syntax errors
public class Return
{
public static void main(String[] args)
{
int result = max (3,4);
System.out.println(result);
}
public static int max(int num1, int num2){
int result;
if (num1 > num2)
result = num1;
else
result = num2;
return result;
}
}
I suggest you should read some basic concepts of programming and language you are using.
But let me try to help you. Your code should look something like:
public static int max(int num1, int num2) {
int result;
if (num1 > num2)
result = num1;
else
result = num2;
return result;
}
public static void main(String[] args) {
System.out.println(max(1, 2));
}
Mistakes in your code were:
max method declared inside main.
arguments are passed inside quotes "int num1, int num2" which is wrong.
No definition of max
not calling max from main
I hope it helped to understand issues with the code.
This short code will return a larger one from two integers.
public static int larger(int a, int b)
{
return a >= b ? a : b;
}
Copy paste this method to your desired class and call this method
larger(12, 7);
Given your class:
public class Return
{
public static int larger(int a, int b)
{
return a >= b ? a : b;
}
public static void main(String[] args)
{
int larger127 = larger(12,7);
System.out.println("The larger int from 12 and 7 is: " + larger127);
}
}
You cannot have a method inside another method. Do it like this:
public static void main(String[] args) {
int result = max (3,4);
System.out.println(result);
}
private static int max(int i, int j) {
return i > j ? i : j;
}
max uses ternary operator to find the largest of the two numbers.

How to create a program that adds subtracts devides and multiplies 2 fractions?

first i would like to start saying that i am new to programing and i don t know much. with that said i would appreciate if anyone could help me with my program that it is supposed to read 2 fractions and an operator for example "2/3 + 4/5". i have some of the code done but it still give me an error when i run it here is what i have so far:
public class Fraction {
private static int numer;
private static int denom;
public Fraction(int num, int den)
{
numer = num;
denom = den;
simplify();
}
int findGcd(int a, int b)
{
int temp;
while(b != 0)
{
temp = b;
b = a % b;
a = temp;
}
return a;
}
void simplify()
{
int gcd = findGcd(numer, denom);
numer /= gcd;
denom /= gcd;
}
public int getNumer(){
return numer;
}
public int getDenom(){
return denom;
}
Fraction add(Fraction x) {
Fraction result;
if (x.getDenom()== getDenom()) {
result = new Fraction(x.getNumer() + getNumer(), denom);
} else {
denom = this.getDenom() * x.getDenom();
numer = this.getNumer() * x.getDenom() + x.getNumer() * this.getDenom();
return new Fraction(numer,denom);
}
return result;
}
public String toString(){
return (Integer.toString(numer) + "/" +
Integer.toString(denom));
}
public static void main (String []args){
Fraction a = new Fraction(1,3);
Fraction b = new Fraction(4,5);
System.out.println(a.toString());
System.out.println(b.toString());
}
}
thank you for your help i really appreciate it.
Why are you making your fields static? static fields belong to the class as opposed to each instantiation (not what you want here). Try removing the static keyword.
On another note, you mentioned that you'd like to read input from the user. You might want to look into using a Scanner for this (in case you don't already know about this handy class).
Once you read the input, something like 2/3 + 4/5, you can split your string using a space as your delimiter. Now, you can parse a fraction from the first (2/3) and third (4/5) elements of the newly formed string array, and perform the operation that corresponds to the second element of the array (+).
There is a difference between static variable and instance variable.
Static variable is a class variable which is common for all instances.. So, if you change these variables for one instance, it will be changed for all the instances.
Instance variable, on the other hand, are specific to each instance.. They are binded to an instance of a class.
That being said.. You need to modify your code a little bit..
Change your static variables in your class to instance variables..
private static int numer;
private static int denom;
The above two variables should be instance variables.. So that they are unique for each instance you create for your class..
So, change them to: -
private int numer;
private int denom;
And for reading user input, A.R.S has already given you link to a valuable class..

Categories

Resources