Please help me to answer the 5 programming questions and give some specific explanations, thanks a lot !!!
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.
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, 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 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 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 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 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: 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 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 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 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...