Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Need help on this with comments please, thanks In this lab you will create two c

ID: 3560956 • Letter: N

Question

Need help on this with comments please, thanks

In this lab you will create two classes, one a superclass of the other. You will observer how methods are inherited and used through the subclass.

Requirements

The archive LengthScaledLengthLab-part1-140725.zip (please, see below)has the source code for a project. Your job is to complete the classes Length and ScaledLength so that the tests work.

The class Length should have two instance fields, named value and unit. value is a decimal number while unit is a string.

The class ScaledLength extends Length. ScaledLength has one instance variable named scale. scale is a decimal number.

Both classes should have getters and setters for all the instance variables.

The Length class should have a method named combined which returns the value concatenated with the unit. The method takes no parameters and returns a String.

Test programs are in the test folder.

Source code for a project

package cs2302.measures;

public class Length {

}

package cs2302.measures;

public class ScaledLength {

}

Test programs

package cs2302.measures.test;

import cs2302.testing.Tests;

public class AllTests {

public static void main(String[] args) {

        Tests.initialize();

        runTests();

        Tests.summary();

    }

public static void runTests() {

        TestStructure.runTests();

        TestLengthAndScaledLength.runTests();

    }

}

package cs2302.measures.test;

import cs2302.measures.Length;

import cs2302.measures.ScaledLength;

import cs2302.testing.Tests;

import static cs2302.testing.Tests.assertEquals;

public class TestLengthAndScaledLength {

private static Length len1;

            private static double value1 = 1.2;

            private static String unit1 = "mm";

            private static Length len2;

            private static double value2 = 2.3;

            private static String unit2 = "in";

            private static String comb2 = "2.3in";

            private static ScaledLength sc1;

            private static double svalue1 = 3.4;

            private static String sunit1 = "pc";

            private static double sscale1 = 4.5;

            private static ScaledLength sc2;

            private static double svalue2 = 5.6;

            private static String sunit2 = "furlong";

            private static double sscale2 = 6.7;

            private static String scomb2 = "5.6furlong";

            // precision for comparing double values

            private static final double EPS = 1e-15;

public static void main(String[] args) {

        Tests.initialize();

        runTests();

        Tests.summary();

    }

public static void runTests() {

        testLengthGetsAndSets();

        testMethodCombined();

        testScaledLengthGetsAndSets();

    }

public static void setup() {

                        len1 = new Length();

                        len1.setUnit(unit1);

                        len1.setValue(value1);

                        len2 = new Length();

                        len2.setUnit(unit2);

                        len2.setValue(value2);

                        sc1 = new ScaledLength();

                        sc1.setUnit(sunit1);

                        sc1.setValue(svalue1);

                        sc1.setScale(sscale1);

                        sc2 = new ScaledLength();

                        sc2.setUnit(sunit2);

                        sc2.setValue(svalue2);

                        sc2.setScale(sscale2);

            }

public static void testLengthGetsAndSets() {

        setup();

                        assertEquals("setValue or getValue in Length may have an error",value1, len1.getValue(),EPS);

                        assertEquals("setUnit or getUnit in Length may have an error",unit1, len1.getUnit());

            }

            public static void testScaledLengthGetsAndSets() {

        setup();

assertEquals("setValue or getValue in ScaledLength may have an error",svalue1, sc1.getValue(),EPS);

                        assertEquals("setUnit or getUnit in ScaledLength may have an error",sunit1, sc1.getUnit());

        assertEquals("setScale or getScale in ScaledLength may have an error",sscale1, sc1.getScale());

    }

            public static void testMethodCombined() {

        setup();

                        assertEquals("combine in Length may have an error",comb2, len2.combined());

                        assertEquals("combine in ScaledLength may have an error",scomb2, sc2.combined());

            }

            }

package cs2302.measures.test;

import java.lang.reflect.Constructor;

import java.lang.reflect.Field;

import java.lang.reflect.Method;

import java.lang.reflect.Modifier;

import static cs2302.testing.Tests.assertEquals;

import static cs2302.testing.Tests.assertNotSame;

import cs2302.testing.Tests;

import cs2302.measures.Length;

import cs2302.measures.ScaledLength;

/**

* This tests whether the class Person has the right fields and methods.

*

* @author Ben Setzer

*

*/

public class TestStructure {

public static void main(String[] args) {

        Tests.initialize();

        runTests();

        Tests.summary();

    }

public static void runTests() {

        testFields();

        testConstructors();

        testMethods();

    }

public static void testFields() {

        Tests.countFields(Length.class, 2);

        Tests.countFields(ScaledLength.class,1);

    }

public static void testConstructors() {

        Tests.countConstructors(Length.class, 1);

        Tests.countConstructors(ScaledLength.class,1);

    }

public static void testMethods() {

        Tests.countMethods(Length.class, 5);

        Tests.countMethods(ScaledLength.class,2);

    }

}

package cs2302.testing;

import java.lang.reflect.Constructor;

import java.lang.reflect.Field;

import java.lang.reflect.Method;

import java.lang.reflect.Modifier;

/**

* Created by Ben on 7/19/2014.

*/

public class Tests {

private static int countSuccesses;

    private static int countFailures;

public static void initialize() {

        countSuccesses = 0;

        countFailures = 0;

    }

public static void assertEquals(String message, int expected, int actual) {

        if(expected != actual) {

            System.out.println("Test failed: expected was not equal to actual");

            System.out.printf("Expected = %d and actual = %d ", expected, actual);

            System.out.println(message);

            countFailures++;

        } else {

            countSuccesses++;

        }

    }

public static void assertEquals(String message, double expected, double actual, double EPS) {

        if(Math.abs(expected - actual) > EPS) {

            System.out.println("Test failed: expected was not equal to actual");

            System.out.printf("Expected = %d and actual = %d ", expected, actual);

            System.out.println(message);

            countFailures++;

        } else {

            countSuccesses++;

        }

    }

public static void assertEquals(String message, Object expected, Object actual) {

        if(!expected.equals(actual)) {

            System.out.println("Test failed: expected was not equal to actual");

            System.out.printf("Expected = %d and actual = %d ", expected, actual);

            if(message != null) {

                System.out.println(message);

            }

            countFailures++;

   } else {

            countSuccesses++;

        }

    }

   public static void assertEquals(int expected, int actual) {

        assertEquals(null, expected, actual);

    }

public static void assertNotSame(String message, int expected, int actual) {

        if(expected == actual) {

            System.out.println("Test failed: expected was equal to actual");

            System.out.printf("Expected = %d and actual = %d ", expected, actual);

            System.out.println(message);

            countFailures++;

        } else {

            countSuccesses++;

        }

    }

public static void countFields(Class<?> cl, int numberOfFields) {

                        Field[] fields = cl.getDeclaredFields();

                        assertEquals("The class '" + cl.getName() + "' should have exactly " + numberOfFields + " fields.",

                                                numberOfFields, fields.length);

                        for(Field f : fields ) {

                                    assertNotSame("The instance field '" + f.getName()

                                                                        + "' in class '" + cl.getName() + "' should be private.",

                                                            0, f.getModifiers() & Modifier.PRIVATE);

                        }

            }

            /**

            * The number of constructors is checked and each constructor is checked

            *        to be public.

            */

            public static void countConstructors(Class<?> cl, int numberOfConstructors) {

                        Constructor<?>[] constructors = cl.getDeclaredConstructors();

                        assertEquals("The class '" + cl.getName() + "' should have exactly " + numberOfConstructors

                        + " constructors.",

                numberOfConstructors, constructors.length);

                        for (int i = 0; i < constructors.length; i++) {

                                    assertNotSame("The constructor " + "in class '" + cl.getName() + "' should be public.",

                                    0, constructors[0].getModifiers() & Modifier.PUBLIC);

                        }

}

/**

* Count the methods and compare to the number specified.

            */

            public static void countMethods(Class<?> cl, int numberOfMethods) {

                        Method[] methods = cl.getDeclaredMethods();

                        assertEquals("The class '" + cl.getName() + "' should have exactly "

                                                + numberOfMethods + " methods.",

                                                numberOfMethods, methods.length);

            }

public static void summary() {

        System.out.println(" ------------ ");

        if(countSuccesses > 0) {

            System.out.println("There were " + countSuccesses + " successful tests");

        } else if(countSuccesses == 1) {

            System.out.println("There was " + countSuccesses + " successful test");

        }

        if(countFailures > 1) {

            System.out.println("There were " + countFailures + " failed tests");

        } else if(countFailures == 1) {

            System.out.println("There was " + countFailures + " failed test");

        }

    }

}

Explanation / Answer

package cs2302.measures;

// Length class
public class Length
{
// two instance fields, named value and unit. value is a decimal number while unit is a string.
privateString unit;
private float value;

// getters and setters for all the instance variables
public String getUnit() {
return unit;
}

public void setUnit(String unit) {
this.unit = unit;
}

public float getValue() {
return value;
}

public void setValue(float value) {
this.value = value;
}
  
// method named combined which returns the value concatenated with the unit.
//The method takes no parameters and returns a String.
  
public String combined()
{
return "Value : "+value+" "+"Unit : "+unit;
}

}

package cs2302.measures;

// ScaledLength class
public class ScaledLength extends Length
{
// one instance variable named scale. scale is a decimal number
float scale;

// getters and setters for scale
public float getScale() {
return scale;
}

public void setScale(float scale) {
this.scale = scale;
}
  
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote