Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Question # 3. * a. Write a program that will write to a text file called output.

ID: 3915597 • Letter: Q

Question

Question # 3. * a. Write a program that will write to a text file called output.txt the message "Hello World * here are the numbers 1 through 10"? Then displaying the numbers 1-10 with each number having its own line. * without using exception handling. * b.re-write the program using exception handling. * --------------------------------------------------------------------------------------------------------- * public static void main(String[] args) { } * --------------------------------------------------------------------------------------------------------- * Question # 4. Write a program that will read from the file provided and display the contents to the * console. In the catch block display to the user an error message that the file could not be read. * --------------------------------------------------------------------------------------------------------- * * public static void main(String[] args) { } * * --------------------------------------------------------------------------------------------------------- * Question # 5. Finish the code by displaying the contents of the two dimensional array in ascending order using only loops. * --------------------------------------------------------------------------------------------------------- * * public static void main(String[] args){ int[][] numbers = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11, 12}}; } * * * ---------------------------------------------------------------------------------------------------------- * Question # 6.Finish the code by writing a switch statement that will give the user legos if they are 6, a * bike if they are 12 and a new car if they are 18, if none of these conditions are met the user gets nothing. * --------------------------------------------------------------------------------------------------------- * * public static void main(String[] args){ int age; Scanner input = new Scanner(System.in); System.out.println("Please enter your age."); age = input.nextInt(); } * ---------------------------------------------------------------------------------------------------------- * Question # 7.Finish the code by writing if-else statements that will. * 1. Display the word baby if the user is or between 0 and 3. * 2. Display the word child if the user is or between 4 and 9. * 3. Display the word pre-teen if the user is or between 10 and 13. * 4. Display the word teen if the user is or between 14 an 17. * 5. Display the word young adult if the user is or between 18 and 23. * 6. if none of the above conditions are met then they are an adult. * --------------------------------------------------------------------------------------------------------- * public static void main(String[] args){ int age; Scanner input = new Scanner(System.in); System.out.println("Please enter your age."); age = input.nextInt(); }

Explanation / Answer

If you have any doubts, please give me comment...

3

a)

public static void main(String[] args) {

PrintWriter out = new PrintWriter(new File("output.txt"));

for(int i=1; i<10; i++){

out.println(i);

}

}

3b)

public static void main(String[] args) {

try{

PrintWriter out = new PrintWriter(new File("output.txt"));

for(int i=1; i<10; i++){

out.println(i);

}

}

catch(FileNotFoundException e){

System.out.println("Unable to write to file");

}

}

4)

public static void main(String[] args) {

try{

int num;

Scanner fin = new Scanner(new File("input.txt"));

if(fin.hasNextInt()){

num = fin.nextInt();

System.out.println(num);

}

}

catch(FileNotFoundException e){

System.out.println("File could not be read");

}

}

5)

public static void main(String[] args) {

int[][] numbers = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11, 12}};

for(int i=0; i<numbers.length; i++){

for(int j=0; j<numbers[i].length; j++){

for(int m=i; m<numbers.length; m++){

for(int n=j; n<numbers[m].length; n++){

if(numbers[i][j]>numbers[m][n]){

int t = numbers[i][j];

numbers[i][j] = numbers[m][n];

numbers[m][n] = t;

}

}

}

}

}

for(int i=0; i<numbers.length; i++){

for(int j=0; j<numbers[i].length; j++){

System.out.print(numbers[i][j]+" ");

}

System.out.println();

}

}

6)

public static void main(String[] args) {

int age;

Scanner input = new Scanner(System.in);

System.out.println("Please enter your age.");

age = input.nextInt();

switch (age) {

case 6:

System.out.println("Legos");

case 12:

System.out.println("Bike");

case 18:

System.out.println("Car");

default:

System.out.println("nothing");

}

}

7)

public static void main(String[] args) {

int age;

Scanner input = new Scanner(System.in);

System.out.println("Please enter your age.");

age = input.nextInt();

if(age>=0 && age<=3)

System.out.println("baby");

else if(age>=4 && age<=9)

System.out.println("child");

else if(age>=10 && age<=13)

System.out.println("pre-teen");

else if(age>=14 && age<=17)

System.out.println("teen");

else if(age>=18 && age<=23)

System.out.println("young adult");

else

System.out.println("adult");

}