1. Write a Java program which reads a text file called mydata.txt containing the
ID: 3767979 • Letter: 1
Question
1. Write a Java program which reads a text file called mydata.txt containing the following numbers: 34 56 76 98 12 33 58 42 98 93 and computes the minimum, maximum sum and average of the numbers and puts the output in a file called myresults.txt.
2. Write a Java Program that prints out Division by Zero and Array Out of Bounds Exceptions when a User attempts to find all integers from 1 to 200 whose last digit is 7, but which are not divisible by 3.
3. Implement the following a Customer Address Book using the Array List data structure. The Customer Address Book should do the following: Add Name, Address and Phone Number to Customer Address Book, Edit Name and Phone Number, Delete an Entry, Display all Names and Phone numbers in the Address Book.
4. Write a Java Program which prints out all solutions of the 8-Queen Problem, but modify the program to display only those solutions which are not on the 3th row or 7th column of the 8x8 matrix.
5. Write a Java Program which implements a Stack of Integers. An Integer should only be pushed into the stack if it is odd or contains the number 3.Now pop all integers that contain 5 or 9. Do this for the first 100 numbers. Print out the numbers remaining in the stack after the Pop operation.
Explanation / Answer
Answer:
Program 1:
//MINMAX.java
import java.io.File;
import java.io.PrintWriter;
import java.util.Scanner;
public class MINMAX
{
public static void main(String args[]) throws Exception
{
// declare the required variables
Scanner file;
int max, min;
double total = 0, average;
int arrays[] = new int[10];
int count = 0;
// create a file object to read
file = new Scanner(new File("mydata.txt"));
// read the values from the file
while (file.hasNextLine())
{
arrays[count] = file.nextInt();
count++;
}
file.close();
// initialize the min and max values
min = arrays[0];
max = arrays[0];
// logic to find the total, minimum and
// and maximum values
for (int i = 0; i < count; i++)
{
total += arrays[i];
if (arrays[i] < min)
min = arrays[i];
if (arrays[i] > max)
max = arrays[i];
}
// find the average of the values
average = total / count;
// Create a PrintWriter object to write the result
PrintWriter pw = new PrintWriter(new File("myresult.txt"));
// write the results in to the text file
pw.println("The elements in the text file are: ");
for (int i = 0; i < count; i++)
{
pw.print(arrays[i] + " ");
}
pw.println(" ");
pw.println("Minimum element is: " + min);
pw.println("Maximum element is: " + max);
pw.println("Sum of elements is: " + total);
pw.println("Average of elements is: " + average);
pw.close();
}
}
Sample Output:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.