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
What will be the output of the given code? using System; class MyProgram { static void...
What will be the output of the given code? using System; class MyProgram { static void Main(string[] args) { try { int a, b; b = 0; a = 5 / b; Console.WriteLine("No exception will occur."); } catch (ArithmeticException e) { Console.WriteLine("Exception occurs."); } finally { Console.WriteLine("Program is executed."); } Console.ReadLine(); } } A Program is executed. B No exception will occur. C Exception occurs. Program is executed. D No exception will occur. Program is executed.
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...
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {       ...
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {        Stack<Integer> new_stack = new Stack<>();/* Start with the empty stack */        Scanner scan = new Scanner(System.in);        int num;        for (int i=0; i<10; i++){//Read values            num = scan.nextInt();            new_stack.push(num);        }        int new_k = scan.nextInt(); System.out.println(""+smallerK(new_stack, new_k));    }     public static int smallerK(Stack s, int k) {       ...
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...
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...
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 =...
1) Consider the following Java program, which one of the following best describes "setFlavor"? public class...
1) Consider the following Java program, which one of the following best describes "setFlavor"? public class Food {     static int count;     private String flavor = "sweet";     Food() { count++; }     void setFlavor(String s) { flavor = s; }     String getFlavor() { return flavor; }     static public void main(String[] args) {         Food pepper = new Food();         System.out.println(pepper.getFlavor());     } } a. a class variable b. a constructor c. a local object variable d....
JAVA public class Purchase { private String name; private int groupCount; //Part of price, like the...
JAVA public class Purchase { private String name; private int groupCount; //Part of price, like the 2 in 2 for $1.99. private double groupPrice; //Part of price, like the $1.99 in 2 for $1.99. private int numberBought; //Total number being purchased. public Purchase () { name = "no name"; groupCount = 0; groupPrice = 0; numberBought = 0; } public Purchase (String name, int groupCount, double groupPrice, int numberBought) { this.name = name; this.groupCount = groupCount; this.groupPrice = groupPrice; this.numberBought...
Getting the following errors: Error 1 error C2436: '{ctor}' : member function or nested class in...
Getting the following errors: Error 1 error C2436: '{ctor}' : member function or nested class in constructor initializer list on line 565 Error 2 error C2436: '{ctor}' : member function or nested class in constructor initializer list on line 761 I need this code to COMPILE and RUN, but I cannot get rid of this error. Please Help!! #include #include #include #include using namespace std; enum contactGroupType {// used in extPersonType FAMILY, FRIEND, BUSINESS, UNFILLED }; class addressType { private:...
1) Consider the following Java program. Which statement updates the appearance of a button? import java.awt.event.*;...
1) Consider the following Java program. Which statement updates the appearance of a button? import java.awt.event.*; import javax.swing.*; public class Clicker extends JFrame implements ActionListener {     int count;     JButton button;     Clicker() {         super("Click Me");         button = new JButton(String.valueOf(count));         add(button);         button.addActionListener(this);         setSize(200,100);         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);         setVisible(true);     }     public void actionPerformed(ActionEvent e) {         count++;         button.setText(String.valueOf(count));     }     public static void main(String[] args) { new Clicker(); } } a. add(button);...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT