Question

Question Consider a class hierarchy that includes a class called Creature, with subclasses called Unicorn and Dragon. The Cre

Question 2 You have been tasked with writing a program that expects user input and, as always users do not always enter the r

Question 3 Given the following class: Not yet answered public class Utility Marked out of 3.00 / The method returns true if t

Question 4 Why does Trent thinks that this cartoon is funny? Discuss in your answer the different tasks associated with using

Question 5 In the following method, describe which variables are thread-safe and which are not thread-safe and why. Not yet a

Please help me to answer the 5 programming questions and give some specific explanations, thanks a lot !!!

Question Consider a class hierarchy that includes a class called Creature, with subclasses called Unicorn and Dragon. The Creature class has a method called feelsLike, Not yet answered which is overridden in the Unicorn and Dragon class. Marked out of The feelsLike method of the Creature class returns "smooth", while the feelsLike method is overridden in the Unicorn class to return "fluffy" and feels Like method 3.00 is overriden in the Dragon to return "scaly" Flag question What is the output of the following snippet of code? Explain your answer. If there is an error, how would you fix it? ArrayList c = new Array List ( ) ; c.add(new Unicorn ( ) ) ; c.add(new Creature () ) ; c.add(new Dragon ( ) ) ; m.get(m.size) . feels Like ( ) ); System.out.println ("Last creature goes В 1 A E
Question 2 You have been tasked with writing a program that expects user input and, as always users do not always enter the right sort of information. As a consequence Not vet answered some of the code you have written will cause some methods calls to throw exceptions. Some of these methods throw unchecked exceptions whilst others return an invalid value (e.g. -1 or null). What is the differences between a checked and unchecked exception? Why do some methods return invalid values rather than Marked out of throwing an exception? 3.00 PFlag question AA
Question 3 Given the following class: Not yet answered public class Utility Marked out of 3.00 / The method returns true if the ArrayList contains the String value, false otherwise arr Flag question Throws a Null PointerException if the ArrayList arr is null public boolean hasValue(Array List arr, String value) throws Nul1 Pointer Exception # code omitted Complete the following test code so that it thoroughly tests the method hasValue so it behaves as described. ALSO, explain your choice of tests. public class Utility Test @Test public void testHasValue complete this test method, making use of asssert method like assertEquals(...,.. .) AA B I
Question 4 Why does Trent thinks that this cartoon is funny? Discuss in your answer the different tasks associated with using a version control system. Not yet answered Marked out of JIM, COULD YOU COME То ME FOR A МОMENT? 3.00 P Flag question OF COURSE WHAT'S UP? NOTHING, THANKS! JUST WANTED TO MAKE SURE THAT I'M THE FIRST TO CHECK IN THE FILES WE'VE BOTH EDITED СНАРТER 1: НOW TO AVOID MERGE CONFLICTS
Question 5 In the following method, describe which variables are thread-safe and which are not thread-safe and why. Not yet answered public int wasSoHard (int x, String in, Array List stuff) { Marked out of char c in.charAt (x) 3.00 P Flag question for (int i= 0; i
0 0
Add a comment Improve this question Transcribed image text
Answer #1

1.

The output of this code is:

"scaly"

This is because the feelslike() method of class Dragon() get called.

2.

Checked: are the exceptions that are checked at compile time. If some code within a method throws a checked exception, then the method must either handle the exception or it must specify the exception using throws keyword.

For example, consider the following Java program that opens file at location “C:\test\a.txt” and prints the first three lines of it. The program doesn’t compile, because the function main() uses FileReader() and FileReader() throws a checked exception FileNotFoundException. It also uses readLine() and close() methods, and these methods also throw checked exception IOException

import java.io.*;

class Main {
   public static void main(String[] args) {
       FileReader file = new FileReader("C:\\test\\a.txt");
       BufferedReader fileInput = new BufferedReader(file);
      
       // Print first 3 lines of file "C:\test\a.txt"
       for (int counter = 0; counter < 3; counter++)
           System.out.println(fileInput.readLine());
      
       fileInput.close();
   }
}

Output:

Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - 
unreported exception java.io.FileNotFoundException; must be caught or declared to be 
thrown
    at Main.main(Main.java:5)

To fix the above program, we either need to specify list of exceptions using throws, or we need to use try-catch block. We have used throws in the below program. Since FileNotFoundException is a subclass of IOException, we can just specify IOException in the throws list and make the above program compiler-error-free.

import java.io.*;

class Main {
   public static void main(String[] args) throws IOException {
       FileReader file = new FileReader("C:\\test\\a.txt");
       BufferedReader fileInput = new BufferedReader(file);
      
       // Print first 3 lines of file "C:\test\a.txt"
       for (int counter = 0; counter < 3; counter++)
           System.out.println(fileInput.readLine());
      
       fileInput.close();
   }
}

Unchecked are the exceptions that are not checked at compiled time. In C++, all exceptions are unchecked, so it is not forced by the compiler to either handle or specify the exception. It is up to the programmers to be civilized, and specify or catch the exceptions.
In Java exceptions under Error and RuntimeException classes are unchecked exceptions, everything else under throwable is checked.

Consider the following Java program. It compiles fine, but it throws ArithmeticException when run. The compiler allows it to compile, because ArithmeticException is an unchecked exception.

class Main {
public static void main(String args[]) {
   int x = 0;
   int y = 10;
   int z = y/x;
}
}

Since exceptions, if not caught properly, result in run time failure, some programmers prefer returning an invalid value.

NOTE: As per HOMEWORKLIB POLICY, I am allowed to answer only 2 questions (including sub-parts) on a single post. Kindly post the remaining questions separately and I will try to answer them. Sorry for the inconvenience caused.

Add a comment
Know the answer?
Add Answer to:
Please help me to answer the 5 programming questions and give some specific explanations, thanks a...
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
  • Please help me to answer the 5 programming questions and give some specific explanations, thanks a...

    Please help me to answer the 5 programming questions and give some specific explanations, thanks a lot !!! Question 5 In the following method, describe which variables are thread-safe and which are not thread-safe and why. Not yet answered public int wasSoHard (int x, String in, Array List<Integer> stuff) { Marked out of char c in.charAt (x) 3.00 P Flag question for (int i= 0; i < in.length); i if (in.charAt (i) == c) { stuff.add (i); return stuff .size()...

  • please answer all questions thanks Question 21 Not yet In a linked-node implementation of a ADT,...

    please answer all questions thanks Question 21 Not yet In a linked-node implementation of a ADT, node does not reference the data in another node answered Points out of 2.00 Select one: True P Flag question False Question 23 Not yet An array implementation of an ADT potentially wastes more space than a linked implementation answered Points out of 2.00 Select one: True P Flag question False Question 25 Should Node be a public class? Briefly explain Not yet answered...

  • Question 1 Not yet answered Marked out of 1.00 Flag question Question text Which of the...

    Question 1 Not yet answered Marked out of 1.00 Flag question Question text Which of the following keywords is useful for skipping to the next iteration of a loop? Select one: a. do b. break c. switch d. continue e. while Clear my choice Question 2 Not yet answered Marked out of 1.00 Flag question Question text Consider the following line of Java code. System.out.println("Hello, World!"); "out" is which of the following? Select one: a. a statement b. a class...

  • Need some help on the following problem in java. I have to modify the code I...

    Need some help on the following problem in java. I have to modify the code I written so far(below) to do the following three things: a). Write a method called diagnostics that occasionally throws any one of the exceptions you have created depending on the values generated by a random-number generator. b). Write a method called display that calls diagnostics and provides exception handlers to display a message when an exception occurs. Otherwise, if an exception does not occur, method...

  • Can someone help me with this code, I not really understanding how to do this? import...

    Can someone help me with this code, I not really understanding how to do this? import java.util.ArrayList; import java.util.HashMap; import java.util.Map; /** * @version Spring 2019 * @author Kyle */ public class MapProblems { /** * Modify and return the given map as follows: if the key "a" has a value, set the key "b" to * have that value, and set the key "a" to have the value "". Basically "b" is confiscating the * value and replacing it...

  • I just started working on some code for a password creator. I was doing some testing and I keep g...

    I just started working on some code for a password creator. I was doing some testing and I keep getting this error: Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 6 at java.base/java.lang.StringLatin1.charAt(StringLatin1.java:47) at java.base/java.lang.String.charAt(String.java:693) at PasswordCreater.check(PasswordCreater.java:56) at Password.main(Password.java:23 In my code I am having the user create a password that is at least 6 characters long, but no more than 16. Also, include at least one lowercase letter, one uppercase letter, one number digit and one special...

  • can you please answer these questions for an assignment Thanks An arithmetic gradient series Select one:...

    can you please answer these questions for an assignment Thanks An arithmetic gradient series Select one: a. starts at zero at the end of the second period and then increases by a constant amount each period. b. starts at zero at the end of the first period and then increases by a constant amount each period. c. starts at zero at the end of the first period and then increases by an increasing amount each period. d. starts at zero...

  • please be thorough with explanations. thank you Question 2 Consider the implementation we made in class...

    please be thorough with explanations. thank you Question 2 Consider the implementation we made in class for ArrayList, and its extensions you did in the lab. In this question, we will add two more methods to this class: the insert method and the pop method. For this question, please submit the modified ArrayList class. a) Implement the method insert (self, index, val) that inserts val before index (shifting the elements to make room for val). For example, your implementation should...

  • I was wondering if I could get some help with a Java Program that I am...

    I was wondering if I could get some help with a Java Program that I am currently working on for homework. When I run the program in Eclipse nothing shows up in the console can you help me out and tell me if I am missing something in my code or what's going on? My Code: public class Payroll { public static void main(String[] args) { } // TODO Auto-generated method stub private int[] employeeId = { 5658845, 4520125, 7895122,...

  • Programming Assignment #7 (Recursion) This assignment is to write some methods that perform simple array operations...

    Programming Assignment #7 (Recursion) This assignment is to write some methods that perform simple array operations recursively. Specifically, you will write the bodies for the recursive methods of the ArrayRecursion class, available on the class web page. No credit will be given if any changes are made to ArrayRecursion.java, other than completing the method bodies Note that the public methods of ArrayRecursion – contains(), getIndexOfSmallest(), and sort() – cannot be recursive because they have no parameters. Each of these methods...

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