Question

using System; public static class Lab5 { public static void Main() { // declare variables int...

using System;
public static class Lab5
{
    public static void Main()
    {
        // declare variables
        int inpMark;
        string lastName;
        char grade = ' ';

        // enter the student's last name
        Console.Write("Enter the last name of the student => ");
        lastName = Console.ReadLine();
        // enter (and validate) the mark
        do {
            Console.Write("Enter a mark between 0 and 100 => ");
            inpMark = Convert.ToInt32(Console.ReadLine());
        } while (inpMark < 0 || inpMark > 100);

        // Use the method to convert the mark into a letter grade
        grade = MarkToGrade();

        // print out the results
        Console.WriteLine("{0}'s mark of {1} converts to a {2}", lastName,
                 inpMark, grade);
        Console.ReadLine();
    }

    // Method: MarkToGrade
    // Parameter
    //      mark: call by value parameter to be converted to a letter grade
    // Returns: the corresponding letter grade
    public static char MarkToGrade(int mark)
    {
        char letterValue;

        // multi-alternative If statement to determine letter grade
        if(mark > 0 && mark < 50)
            letterValue = 'F';
        else if(mark >= 50 && mark < 60)
            letterValue = 'D';
        else if(mark >= 60 && mark < 75)
            letterValue = 'C';
        else if(mark >= 75 && mark < 85)
            letterValue = 'B';
        else if(mark >= 85 && mark <= 100)
            letterValue = 'A';
        else
        {
            Console.WriteLine("***Error - invalid mark");
            letterValue = 'X';
        }

        //return the letter grade back to Main
        return letterValue;
    }
}
  1. . At Line 53, add the following statement:
    Console.WriteLine("The value of the mark is {0}", inpMark);

    What error message did you receive and why?

  2. Remove the extra line you added in Part (1). Now let’s change the method header to include a call by address (reference) parameter. Change Line 33 to be:

    public static void MarkToGrade(int mark, ref char letterValue)

    Remove Lines 35 (char letterValue;) and 55 (return letterValue;))

    Change Line 21 to be:

    MarkToGrade(inpMark, ref grade);Run the program with the input of Smith and 98.

    What is the output?

    What do you notice about the output relative to what you saw in Part (b)?

    Why did we have to remove Line 35 (try putting it back in)?

    What did we have to remove Line 55 (try putting it back in)?

    Why did the method call (invocation) in Line 21 have to change?

  3. One last set of changes to the program from Part (j). In the method call (Line 21), change it to:
    grade = MarkToGrade(inpMark);

    What error message did you receive and why?

    What if we change Line 21 to:

    grade = MarkToGrade(inpMark, ref grade);

    What error message did you receive and why?

    Finally, how about if we change Line 21 to:

    MarkToGrade(ref grade, inpMark);

    What error messages did you receive and why?

Homework Answers

Answer #1

1)

We recieve the following error because inpMark is not defined within the method

error CS0103: The name `inpMark' does not exist in the current context
Compilation failed: 1 error(s), 0 warnings

2)

What is the output?:

Why did we have to remove Line 35 (try putting it back in)? Because we are passing the letterValue as a parameter to the function. we cannot redeclare it in the same context.

What did we have to remove Line 55 (try putting it back in)? we changed the functions' return type to void, so it cannot have a return type

Why did the method call (invocation) in Line 21 have to change? we changed the function header to recieve 2 parameters, so it had to be changed.

3)

What error message did you receive and why? we recieve the following the error because MarkToGrade function expects 2 parameters and we only passed one.
main.cs(21,17): error CS1501: No overload for method `MarkToGrade' takes `1' arguments

What if we change Line 21 .....What error message did you receive and why? We recieve the following error because the MarkToGrade function is of type void and the variable "grade" is of type char, The conversion between the two types can only be explicit

Cannot implicitly convert type `void' to `char'

Finally, how about if we change Line 21......What error messages did you receive and why? We recive the following error because the function expects the parameters in order int, char. but we have swapped them.

The best overloaded method match for `Lab5.MarkToGrade(int, ref char)' has some invalid arguments

----------------------------------------------------------------------------------------

# If you have a query/issue with respect to the answer, please drop a comment. I will surely try to address your query ASAP and resolve the issue

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a method with the following header: public static int getValidInput(int low, int high, String message)...
Write a method with the following header: public static int getValidInput(int low, int high, String message) This method will return a user entered number between high and low inclusive. If a number is entered that is not between high and low, string message will be printed to the screen. If high is less than low, a -1 is returned. For example, given this call: int input = getValidInput(0, 100, “Not a valid grade, Please re- enter”); The following could occur:...
import java.util.Scanner; public class AroundTheClock {    public static void main(String[] args)    {       ...
import java.util.Scanner; public class AroundTheClock {    public static void main(String[] args)    {        Scanner input = new Scanner(System.in);        int departureTime = input.nextInt();        int travelTime = input.nextInt();        int arrivalTime;            departureTime =12;            departureTime = 0;            arrivalTime = (int) (departureTime + travelTime);            (arrivalTime = (arrivalTime >=12));            if (arrivalTime = arrivalTime %12)            {            System.out.println(arrivalTime);...
class Ex1{ 2. public static void main(String args[]){ 3. int x = 10; 4. int y...
class Ex1{ 2. public static void main(String args[]){ 3. int x = 10; 4. int y = new Ex1().change(x); 5. System.out.print(x+y); 6. } 7. int change(int x){ 8. x=12; 9. return x; 10. } 11. } Can you please explain this entire code and what is happening?
public class MyClass {   public static void main(String[] args)   {     for (int x = 1; x...
public class MyClass {   public static void main(String[] args)   {     for (int x = 1; x <= 10; x++)     {       System.out.print("-x" + x);            }   } } If this simple program is compiled and run, the following is written to the standard output.
-x1-x2-x3-x4-x5-x6-x7-x8-x9-x10 Add only one statement to the line 8. As a result of that the program writes “-x1-x7” to the standard output.
import java.util.Scanner; public class InRange { private int min; private int max; public InRange(int initialMin, int...
import java.util.Scanner; public class InRange { private int min; private int max; public InRange(int initialMin, int initialMax) { min = initialMin; max = initialMax; } // You need to write two instance methods: // 1.) A method named inRange, which takes an int. // This returns true if the int is within the given range // (inclusive), else false. // // 2.) A method named outOfRange which takes an int. // This returns false if the int is within the...
Given a class: public class Money { int wholeNumber; int decimalPart; boolean positive; char currencySymbol; }...
Given a class: public class Money { int wholeNumber; int decimalPart; boolean positive; char currencySymbol; } Write a constructor for setting the 4 instance variables (in the order specified above). The constructor must validate the input throwing an IllegalArgumentException (you can specify any error message you see fit) if any of the following conditions are not met: either the int values are negative decimalPart is greater than 99 the currencySymbol is not one of '$', '€', or '£' Write an...
In Chapter 9, you created a Contestant class for the Greenville Idol competition. The class includes...
In Chapter 9, you created a Contestant class for the Greenville Idol competition. The class includes a contestant’s name, talent code, and talent description. The competition has become so popular that separate contests with differing entry fees have been established for children, teenagers, and adults. Modify the Contestant class to contain a field that holds the entry fee for each category, and add get and set accessors. Extend the Contestant class to create three subclasses: ChildContestant, TeenContestant, and AdultContestant. Child...
1.If you have defined a class,  SavingsAccount, with a public  static method,  getNumberOfAccounts, and created a  SavingsAccount object referenced by...
1.If you have defined a class,  SavingsAccount, with a public  static method,  getNumberOfAccounts, and created a  SavingsAccount object referenced by the variable  account20, which of the following will call the  getNumberOfAccounts method? a. account20.getNumberOfAccounts(); b. SavingsAccount.account20.getNumberOfAccounts(); c. SavingsAccount.getNumberOfAccounts(); d. getNumberOfAccounts(); e. a & c f. a & b 2.In the following class, which variables can the method printStats use? (Mark all that apply.) public class Item { public static int amount = 0; private int quantity; private String name; public Item(String inName, int inQty) { name...
What is the output of the following Java program? public class Food {     static int...
What is the output of the following Java program? public class Food {     static int count;     private String flavor = "sweet";     Food() { count++; }     void setFlavor(String s) { s = flavor; }     String getFlavor() { return flavor; }     static public void main(String[] args) {         Food pepper = new Food();         pepper.setFlavor("spicy");         System.out.println(pepper.getFlavor());     } } Select one: a. sweet b. 1 c. The program does not compile. d. 2 e. spicy...
is there anything wrong with the solution. the question are from java course Write a main...
is there anything wrong with the solution. the question are from java course Write a main method that will request the user to enter Strings using a JOptionPane input dialog. The method should continue accepting strings until the user types “STOP”.       Then, using a JOptionPane message dialog, tell the user how many of the strings begin and end with a digit. Answer: import javax.swing.*; public class IsAllLetters {     public static void main(String[] args) {         String input;         int count =...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT