You are on page 1of 4

The exam has a `Main` class with 12 methods. Methods are called `part0`, `part1`, etc.

The body of each method has comments that explain what the method needs to return. Your job is to
write code to make sure it returns the correct value. There are tests you can run that will verify your
method is working properly. Your grade will be determined by how many tests are passing. Each test or
part is worth 5 points, for a total of 60.

Ideally you should -

1. Fix any compiler errors for missing return statements and such.

2. You can verify there are no compiler errors by running `javac Main.java` in the terminal. The green run
button is also configured to run `javac Main.java`. If you see no output, that means there were no
compiler errors.

3. You want to start implementing each part or each method as per the comments, and then run tests to
make sure the tests for that part is passing. Or if you can confident you can run tests once at the end.

4. Once you are done with everything and got as many tests to pass as you could, hit submit.
import java.lang.Math;

public class Main {
    public int part0(int lhs, int rhs) {
        // Complete this method such that returns the sum of the two interger ar
guements.

        // This method as already been implemented for you as an example -
        
        int sum = lhs + rhs; // finding out the sum

        return sum;          // and then returning it
    }

    public String part1() {
        // Declare a String variable, name it student and assign it the value "M
alcolm."

        String student = "Malcolm";
            return student; 

        // once you are done, return the string variable you created on the foll
owing line -
        // hint: return NAME_OF_VARIABLE;
    }

    public int part2(int lhs, int rhs) {
        // Complete this method so it returns out the remainder of the two param
eters.

        int result = lhs%rhs;
        return result; 

        // hint: int result = ???
        // hint: and then: return result; ?
    }

    public int part3(int lhs, int rhs) {
        // Complete this method so it returns the quotient of the two parameters
.

        int result = lhs / rhs;
        return result;

    }
    public int part4(int lhs, int rhs) {
        // Complete this method so it returns the product of the two parameters.

        int result = lhs * rhs;
        return result; 
    }

    public String part5(String lhs, String rhs) {
        // Complete this method to return the two String parameters combined (co
ncatenated) together.
        // the combined string must have exactly one space (that is, this - " ") 
between them.

        String str1 = lhs+" "+rhs;
        return str1;
    }

    public String part6(long val) {
        // Complete this method such that it returns the long integer value as a 
string, but the string should be formatted in a nice way.
        // by nice way, I mean comma-separated; so if `val` (the arguement) is e
qual to `1000`, the method must return "1,000"
        // if `val` is `12141`, the method must return "12,141".

        String result = String.format("%,d",val);
        return result;

        // hint: did we learn about something called String.format()?

        // hint: you can read more at this link - https://dzone.com/articles/
java-string-format-examples#:~:text=The%20most%20common%20way%20of%20formatting
%20a%20string,Formatter%20and%20link%20it%20to%20a%20StringBuilder%20.
    }

    public int part7(String val) {
        // Complete this method such that it returns the index of the first excl
amation mark you can find in the string.

        int value = val.indexOf("!");
        return value;
    }

    public String part8(String val) {
        // Complete this method such that it returns every part of the string ri
ght before the excalamtion mark.
        // for example - If `val` is "Hello! How are you?", the method should re
turn "Hello"
        
        int p = val.indexOf("!");
        String result = val.substring(0,p);
        return result;

        // Note: all strings passed to this method will always contain one excal
amation mark.
    }

    public double part9() {
        // Complete this method such it returns a random decimal value between 0 
and 1

        double num =  Math.random();
        return num;
    }
    
    public int part10() {
        // Complete this method such it returns a random number between 0 and 50
0

        int rand = (int)(Math.random() * (500+1));
        return rand;
  
    }

    public int part11(int start, int end) {
        // Complete this method such it returns a random number between the `sta
rt` integer arguement and the `end` integer arguement.

        int upper = start;
        int lower = end;
        int result = (int) (Math.random() * (upper - lower + 1)) + lower;
        return result;

    }
}

You might also like