Question

For this assignment you are to create two programming projects. The first should have a project...

For this assignment you are to create two programming projects. The first should have a project directory with Lab02-2WriteNumbers and the second should have a project directory with Lab02-2ReadNumbers.

Lab02-03WriteNumbers – Write a program that asks the user to enter five numbers. Use a double data type to hold the numbers. The program should create a file and save all five numbers to the file.

Enter a number (1 of 5): 45

Enter a number (2 0f 5): 23

Enter a number (3 0f 5): 78

Enter a number (4 0f 5): 111

Enter a number (5 0f 5): 27

done

Lab02-03ReadNumbers – Write a program that opens the file created in Lab02-2WriteNumbers, reads the five numbers and displays them. The program should also calculate and display the sum of all the numbers.

//oops I used a different numbers.txt… oh well… you get the idea…

34

111

456

3

67

The total of all the numbers is 671

done

I have the first part done. Just need the second part.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

WriteNumbers.java

import java.util.*;
import java.io.*;

class WriteNumbers
{
public static void main(String args[])
{
double arr[]=new double[5];
int i;

try
{
Scanner sc=new Scanner(System.in);
FileWriter fw=new FileWriter("Numbers.txt");
System.out.println("\n");

for(i=0;i<=4;i++)
{
System.out.print("Enter a number "+"("+(i+1)+" of 5) :");
arr[i]=sc.nextDouble();
}

for(i=0;i<=4;i++)
{
fw.write(arr[i]+"\n");
}

fw.close();
System.out.println("Numbers stored successfully!!\n");
}
catch(Exception e1)
{
System.out.println("Exception is "+e1);
}
}
}

ReadNumbers.java

import java.io.*;

class ReadNumbers
{
public static void main(String args[])
{
try
{
FileReader fr=new FileReader("Numbers.txt");
int i;
String numbers="";
System.out.println("\n");
while((i=fr.read())!=-1)
{
System.out.print((char)i);
numbers=numbers+(char)i;
}

String[] nums=numbers.split("\n");

double total=0;

for(int k=0;k<=4;k++)
{
total=total+Double.parseDouble(nums[k]);
}

System.out.println("The total of all the numbers is "+total);
System.out.println("\n");
fr.close();
}
catch(Exception e1)
{
System.out.println("Exception is "+e1);
}
}
}

Add a comment
Know the answer?
Add Answer to:
For this assignment you are to create two programming projects. The first should have a project...
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
  • I need help in Python. This is a two step problem So I have the code...

    I need help in Python. This is a two step problem So I have the code down, but I am missing some requirements that I am stuck on. Also, I need help verifying the problem is correct.:) 7. Random Number File Writer Write a program that writes a series of random numbers to a file. Each random number should be in the range of 1 through 500. The application should let the user specify how many random numbers the file...

  • Write a python program and pseudocode The Program Using Windows Notepad (or similar program), create a...

    Write a python program and pseudocode The Program Using Windows Notepad (or similar program), create a file called Numbers.txt in your the same folder as your Python program. This file should contain a list on floating point numbers, one on each line. The number should be greater than zero but less than one million. Your program should read the following and display: The number of numbers in the file (i.e. count them) (counter) The maximum number in the file (maximum)...

  • java Part 1 Create a NetBeans project that asks for a file name. The file should...

    java Part 1 Create a NetBeans project that asks for a file name. The file should contain an unknown quantity of double numeric values with each number on its own line. There should be no empty lines. Open the file and read the numbers. Print the sum, average, and the count of the numbers. Be sure to label the outputs very clearly. Read the file values as Strings and use Double.parseDouble() to convert them. Part 2 Create a NetBeans project...

  • please read directions first. this is supposed to be a hybrid programe add comments please JAVA...

    please read directions first. this is supposed to be a hybrid programe add comments please JAVA please add comments Programming Project 1 and Programming Project 2 "Hybrid" Your goal is to write a program that combines the functionality of both Programming Project 1andProgramming Project 2 into a single program. That is, your program should read a file ( Numbers.txt) of numbers of type double that contains some duplicates and is ordered smallest to largest. Your program should ignore the duplicate...

  • Description: Create a program called numstat.py that reads a series of integer numbers from a file...

    Description: Create a program called numstat.py that reads a series of integer numbers from a file and determines and displays the name of file, sum of numbers, count of numbers, average of numbers, maximum value, minimum value, and range of values. Purpose: The purpose of this challenge is to provide experience working with numerical data in a file and generating summary information. Requirements: Create a program called numstat.py that reads a series of integer numbers from a file and determines...

  • Create a java project. Create a class called cls2DarrayProcessor.java. with the following: Reads numbers from a...

    Create a java project. Create a class called cls2DarrayProcessor.java. with the following: Reads numbers from a pre-defined text file. A: Text file name should be data.txt B: Text file should be in the same workspace directory of your project's folder C: Text file name should be STORED in a String and called: String strFile ='./data.txt' Defines a 2-D array of integers with dimensions of 5 rows by 5 columns. Inserts the data from the text file to the 2-D array...

  • Piggy back on the programming project one you completed in module 3. You will use some...

    Piggy back on the programming project one you completed in module 3. You will use some of those concepts here again. We will be building this project inside out, start individual quiz and use a for loop to repeat it three times for three students. In this program you will be required to write a python program that generates math quizzes for students in second grade. Your program should do the following Ask the student for their name Provide 3...

  • Create a New Java Project called YourLastNameUpperCase. Write a program that asks the user for the...

    Create a New Java Project called YourLastNameUpperCase. Write a program that asks the user for the names of two files. The first file should be opened for reading and the second file should be opened for writing. The program should read the contents of the first file, change all characters to uppercase, and store the results in the second file. The second file will be a copy of the first file, except that all the characters will be uppercase. Use...

  • Your mission in this programming assignment is to create a Python program that will take an...

    Your mission in this programming assignment is to create a Python program that will take an input file, determine if the contents of the file contain email addresses and/or phone numbers, create an output file with any found email addresses and phone numbers, and then create an archive with the output file as its contents.   Tasks Your program is to accomplish the following: ‐ Welcome the user to the program ‐ Prompt for and get an input filename, a .txt...

  • You have been hired as a programmer by a major bank. Your first project is a...

    You have been hired as a programmer by a major bank. Your first project is a small banking transaction system. Each account consists of a number and a balance. The user of the program (the teller) can create a new account, as well as perform deposits, withdrawals, and balance inquiries. The application consists of the following functions:  N- New account  W- Withdrawal  D- Deposit  B- Balance  Q- Quit  X- Delete Account Use the following...

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