Question
need java code for this question
Question 2 (15 marks) (a) Does the following class successfully compile? Explain your answer. public class MyClass public sta
0 0
Add a comment Improve this question Transcribed image text
Answer #1

a) The class will be compiled successfully, as there are no syntax errors.

When we pass -10 as an argument, if condition will be true and the program will throw Runtime Exception null as shown in below image.

MyClass X Compile Undo Cut Copy Paste Find... Close 1 public class MyClass 2 { public static void main(String[] args) { if(In

b)

Program is as follows: Try with different values of carValue and driverAge.

CODE:

import java.util.*;
public class MyClass
{
public static double calculateInsurancePremium(double carValue, int driverAge) throws Exception
{
if(carValue < 1000)
throw new InputMismatchException("carValue must be atleast 1000");
  
if(driverAge < 18 || driverAge > 90)
throw new InputMismatchException("Age does not lie in range");
  
double premium;
if(driverAge >= 21)
premium = 0.05 * carValue;
else
premium = 0.10 * carValue;
  
return premium;
}
public static void main(String [] args) throws Exception
{
double cv = 950;
int da = 25;
System.out.println(calculateInsurancePremium(cv, da));
}
}

OUTPUT:

MyClass X Compile Undo Cut Copy Paste Find... Close { 1 import java.util.*; public class My Class 3 { 4 public static double

Add a comment
Know the answer?
Add Answer to:
need java code for this question Question 2 (15 marks) (a) Does the following class successfully...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Question 1 (5 points) Question 1 Unsaved What is displayed on the console when running the...

    Question 1 (5 points) Question 1 Unsaved What is displayed on the console when running the following program? public class Quiz2B { public static void main(String[] args) { try { System.out.println("Welcome to Java"); int i = 0; int y = 2 / i; System.out.println("Welcome to Java"); } catch (RuntimeException ex) { System.out.println("Welcome to Java"); } finally { System.out.println("End of the block"); } } } Question 1 options: The program displays Welcome to Java two times. The program displays Welcome to...

  • Write a unit test to test the following class. Using Java : //Test only the debit...

    Write a unit test to test the following class. Using Java : //Test only the debit method in the BankAccount. : import java.util.*; public class BankAccount { private String customerName; private double balance; private boolean frozen = false; private BankAccount() { } public BankAccount(String Name, double balance) { customerName = Name; this.balance = balance; } public String getCustomerName() { return customerName; } public double getBalance() { return balance; } public void setDebit(double amount) throws Exception { if (frozen) { throw...

  • Question 5 (10 points) Assume you have the following code, with blanks to be filled in...

    Question 5 (10 points) Assume you have the following code, with blanks to be filled in below. public static void g () ( throw new () public static void f() try ) catch ( 12), e) System.out.print ("A") ; return } catch ( System.out. print ("B") throw ei e) ( 13) catch ( 14) e) ( System.out.print ("C") ; } finally System.out.print ("D") ; System.out.print ("E") ; } public static void main (String (] args) try f() } catch (...

  • I must implement a class to calculate n-th row of Pascal's Triangle (in Java). I need...

    I must implement a class to calculate n-th row of Pascal's Triangle (in Java). I need to create a static method that takes an argument "n" and returns the n'th line of pascal's triangle. the main method, which will call the class, is already done and the class is set up. Please build this without modifying the main method and without modifying exactly what the static method returns. public class Main { public static void main(String[] args) { int n...

  • c. [3 marks] Given the following definition of the class MyClass, class MyClass { String s...

    c. [3 marks] Given the following definition of the class MyClass, class MyClass { String s int z Myclass(int y) { z = y } } fill in the blanks (labelled i., ii. and iii.) in the definition of the method main, of the class StringProcessing, with i. a declaration of a variable named obj of type MyClass, initialised with a MyClass object so that its instance variable z has a value of 20, ii. an assignment of the instance...

  • Java Method Resolution : Consider the following example: What is printed on the console if Main...

    Java Method Resolution : Consider the following example: What is printed on the console if Main is executed, and why? How the codes could be improved? public class Main {     public static void main(String[] args) throws Exception {         A obj = null;         obj.foo();     } } public class A {     public void foo() {         System.out.println(“foo in A”);     } }

  • Java please answer A to I please dont type the answer on paper please use the...

    Java please answer A to I please dont type the answer on paper please use the computer A.   Explain why alpha cannot be accessed by other members of its class. B.   In the program, how can you determine the type of access modifier? C.   Describe the differences and similarities of beta and gamma in program, within the context of access specifiers and class member accessibility. D.   Explain how objects, a and b, are passed to the method. E.    Why...

  • [JAVA] Suppose we need to write code that receives String input from a user, and we...

    [JAVA] Suppose we need to write code that receives String input from a user, and we expect the String input to contain a double value (inside quotes). We want to convert the String to a double using the static method Double.parseDouble(String s) from the Double class. The parseDouble method throws a NumberFormatException if the String s is not a parsable double. Write a method printSum that takes two String parameters (that hopefully have parsable doubles in them), and either prints...

  • Question 1 1 pts Which of the following is not a valid class name in Java?...

    Question 1 1 pts Which of the following is not a valid class name in Java? O MyClass MyClass1 My_Class MyClass# Question 2 1 pts Which of the following statements is False about instance variables and methods? Instance variables are usually defined with private access modifier. Instance variables are defined inside instance methods. Instance methods are invoked on an object of the class that contains the methods. A class can have more than one instance variables and methods. Question 3...

  • create a new Java application called "CheckString" (without the quotation marks) according to the following guidelines....

    create a new Java application called "CheckString" (without the quotation marks) according to the following guidelines. ** Each method below, including main, should handle (catch) any Exceptions that are thrown. ** ** If an Exception is thrown and caught, print the Exception's message to the command line. ** Write a complete Java method called checkWord that takes a String parameter called word, returns nothing, and is declared to throw an Exception of type Exception. In the method, check if the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT