write a program to handle an exception that is generated when a program attempts to write beyond the end of an array in a lower scope such that that the exception is handled at a higher scope. java please write simple code so I can understand. use the code below
public class Main
{
public static void main(String [] args)
{
int [] values = {1,2,3,4};
try
{
int theSum= calculate_sum(values);
}
catch (expection arrayindexoutofBoundsexpection)
{
//System.out.println(“tired to access array outside of
bounds”);
//System.exit(-1);
//}
System.out.println(“the Sum is: “ + theSum);
}
public static int calculate_sum(int[] theValues)
{
int total= 0;
for(int i=0;i<6;i++)
total += theValues[i]);
return total;
}
}
}
// Screenshot of the code & output
Code to handle OarrayindexoutofBoundsexpection output

Working code output

// Code to copy
Code to handle OarrayindexoutofBoundsexpection
Main.java
public class Main {
public static void main(String[] args) {
int [] values = {1,2,3,4};
int theSum = 0;
try
{
theSum=
calculate_sum(values);
System.out.println("the Sum is: " +
theSum);
}
catch (Exception
arrayindexoutofBoundsexpection)
{
System.out.println("tired to access
array outside of bounds");
System.exit(-1);
}
}
private static int calculate_sum(int[] theValues)
{
int total= 0;
for(int i=0;i<6;i++) {
total +=
theValues[i];
}
return total;
}
}
Main.java
public class Main {
public static void main(String[] args) {
int [] values = {1,2,3,4};
int theSum = 0;
try
{
theSum=
calculate_sum(values);
System.out.println("the Sum is: " +
theSum);
}
catch (Exception
arrayindexoutofBoundsexpection)
{
System.out.println("tired to access
array outside of bounds");
System.exit(-1);
}
}
private static int calculate_sum(int[] theValues)
{
int total= 0;
for(int
i=0;i<theValues.length;i++) {
total +=
theValues[i];
}
return total;
}
}
write a program to handle an exception that is generated when a program attempts to write...
JAVA CODE This program causes an error and crashes. Compile and give it a test run, note the exception that is thrown. Then do the following to fix the problem 1. Write code to handle this exception 2. Use try, catch and finally blocks. 3. Display, the name of the exception 4. Display a message from the user about what happened. Here is the starting file BadArray.java: public class BadArray { public static void main(String[] args) { // Create an...
JAVA HELP: Directions Write a program that will create an array of random numbers and output the values. Then output the values in the array backwards. Here is my code, I am having a problem with the second method. import java.util.Scanner; import java.util.Random; public class ArrayBackwards { public static void main(String[] args) { genrate(); print(); } public static void generate() { Scanner scanner = new Scanner(System.in); System.out.println("Seed:"); int seed = scanner.nextInt(); System.out.println("Length"); int length = scanner.nextInt(); Random random...
Using java socket programming rewrite the following program to handle multiple clients simultaneously (multi threaded programming) import java.io.*; import java.net.*; public class WelcomeClient { public static void main(String[] args) throws IOException { if (args.length != 2) { System.err.println( "Usage: java EchoClient <host name> <port number>"); System.exit(1); } String hostName = args[0]; int portNumber = Integer.parseInt(args[1]); try ( Socket kkSocket = new Socket(hostName, portNumber); PrintWriter out = new PrintWriter(kkSocket.getOutputStream(), true); BufferedReader in = new BufferedReader( new InputStreamReader(kkSocket.getInputStream())); ) { BufferedReader...
Question 1 8 pts Write all the different possible outputs of the following Java multi-threaded program: class MultithreadingDemo extends Thread public void runot try{ System.out.println (--Multithread.countDown): catch (Exception e) w public class Multithread{ public static int countDown = 10; public static void main(String[] args) { for (int i = 0; i < 3; i++) { MultithreadingDemo object = new MultithreadingDemol); object.start(); } میه
I need to write a program in java that reads a text file with a list of numbers and sorts them from least to greatest. This is the starter file. import java.util.*; import java.io.*; public class Lab3 { static final int INITIAL_CAPACITY = 5; public static void main( String args[] ) throws Exception { // ALWAYS TEST FOR REQUIRED INPUT FILE NAME ON THE COMMAND LINE if (args.length < 1 ) { System.out.println("\nusage: C:\\> java Lab3 L3input.txt\n"); System.exit(0); } //...
Write in Java! Do NOT write two different programs for Deck and Card, it should be only one program not 2 separate ones!!!!!! The Learning Goal for this exercise is to use and understand and know the difference between arrays and array lists. !!!!Use at least one array defined in your code and two array lists defined by the operation of your code!!!! The array should be 52 elements and contain a representation of a standard deck of cards, in...
.) Write a program to implement the exception handling with multiple (at least 3) catch statements. Any type of exception may be thrown. Catch statements must display (cout) the type of exception thrown. .) Write a function which accepts an index and returns the corresponding element from an array. If the index is out of bounds, the function should throw an exception. Handle this exception in "main()". (This is pretty open ended, so anything from a calculator or "what have...
Write a program that stores a phrase as an array of words, and then prints it backwards. The main method calls method getInput , which asks the user how many words there are, stores them in an array, and returns this array. printBackwards then takes this array of words, and prints it in reverse. Code Example: import java.util.Scanner; public class L17Num1{ public static void main(String[] args) { String[] sArray=getInput(); printBackwards(sArray); } public static String[] getInput() { System.out.println("How many...
What are the results of the program as written? What results would the program produce if you changed the value of a from 0 to 3? ************************************************************************************* * Program Summary: * This code includes simple try/catch (exception handling) functionality. * We put some arithmetic code into a "try" block so that we can "catch" an exception * that is thrown during the course working through the arithmetic. * We need to know specifically what kind of exception we intend...
java read integers from this binary file and when the value is 0 then stop reads and print its summation here is my code import java.io.FileOutputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Random; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.FileInputStream; class Main { public static void main(String[] args) { String fname = "out.txt"; prepare(fname); ObjectInputStream inputStream = null; // create code here } public static void prepare(String fname) { ...