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...

  • Please help me answer these questions in java programming language. Please answer it line after line...

    Please help me answer these questions in java programming language. Please answer it line after line so that I can understand please. Does it compile or not? No programming is done in this exercise, simply answer the questions asked in the code given below. class Exemple { /*Explain why this code does not compile */ public void m1() { foo(); } public int foo() throws Exception { throw new Exception(); } /*Explain why this code is not considered as good...

  • 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 (...

  • JAVA Please debug. Has 2 errors. Thanks ---------------------------------------- import javax.swing.*; public class UseCustomerAccount {     public...

    JAVA Please debug. Has 2 errors. Thanks ---------------------------------------- import javax.swing.*; public class UseCustomerAccount {     public static void main(String[] args)     {         int num;         double balance;         String input;                 input = JOptionPane.showInputDialog(null, "Enter an account number");         num = Integer.parseInt(input);         input = JOptionPane.showInputDialog(null, "Enter a balance due");         balance = Double.parseDouble(input);                 try         {             CustomerAccount ca = new CustomerAccount(num, balance);             JOptionPane.showMessageDialog(null, "Customer #" + num + " has a balance of...

  • [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...

  • How many lexemes does the following Java code contain? public class RectArea { public static double...

    How many lexemes does the following Java code contain? public class RectArea { public static double area(double length, double width) { double area = 0.0: try { area = width*height: } catch (Exception e) { System.out.println ("Error in RectArea.area: " + e.getMessage()): System.exit (1): } return area: } } Give the number of lexemes on each line (using the line numbers shown).

  • 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...

  • Question 1 Consider the following code snippet: public class Box<E> { private E data; public Box()...

    Question 1 Consider the following code snippet: public class Box<E> { private E data; public Box() { . . . } public void insert(E value) { . . . } public E getData() { . . . } } What will result from executing the following code? Box<String> box = new Box<>(); . . . box.insert("blue Box"); String b = box.getData(); A.   run-time error B.   compiler warning C.   no error D.   compiler error Question 2 What is used as a...

  • 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