Results 1 to 8 of 8
- 02-28-2012, 06:57 AM #1
Member
- Join Date
- Feb 2012
- Posts
- 5
- Rep Power
- 0
Need Help with Homework Lab Assignment on Loops and File Input/Output
Hello. I am new here and I am currently taking an online Java course in college. I've done well on it so far without having to ask for assistance, but there is this lab assignment that I am trying to complete that is really driving me up the wall. The directions are as follows.
Ok so for the directions call for writing a program with two classes that process a series of numbers imported from a text file and then output to another text file. The text file that I have to import contains the numbers 1271100023007002591123411132408255 all bunched up in one line. Now for the first class that I have to write, am I supposed to read this whole series of numbers in the text files as one big number or as individual digits number by number? The sumOfDigits method implies that I have to read in the series as one number and then add all its digits. However in the data file explanation, I am further confused because how am I supposed to break up the series of numbers into individual numbers that contain one to three digits? I am sorry but I am completely lost here. Has anyone ever had an assignment like this before?Objective: Review looping structures and practice using text files in Java.
At the end of this lab you should know how to:
1. read from a text file
2. write to a text file
3. use a while loop to accomplish a task
4. use a for loop to accomplish a task
Read the entire lab assignment before you begin working on any one part.
About the application program
Write an application program that declares one OneNumber object and one TwoNumber object. (You will also need to declare other variables.) The program should ask the user to enter the input filename and the output filename (use the Scanner class or JOptionPane). The main method should contain a while loop that reads data from the input file. The loop should terminate when there is no more data in the file. The OneNumber object should call appropriate accessor/mutator methods and each of the additional methods required for this particular class. The TwoNumber object should call appropriate accessor/mutator methods and each of the additional methods required for this particular class. The program should output to a file the value(s) of the field(s) and the results of the unique class methods (isPrime, sumOfDigits, divisibleByNine or greatestCommonFactor, sumBetweenNumbers). Make sure that each part of the output is properly labeled and easy to read.
About the first class
Write a class named OneNumber that has one integer field. Use only while or for loops. You should only exit from a loop by a false loop condition —no exit or breaks allowed.
Two constructors: the default constructor and one constructor with one parameter,
Standard mutator and accessor methods,
isPrime – This method should determine if the number read from the file is a prime number, (A prime number is a number divisible by itself and 1.)
sumOfDigits – This method should find the sum of the digits of the number read from the file. (For example: the number 123 would add 1 + 2 + 3 to get 6).
divisibleByNine – This method should determine if the number read from the file is divisible by 9. (Use the fact that a number is divisible by nine when the sum of the digits is divisible by nine.)
About the second class
Write a class named TwoNumber.that has two integer fields. Use only while or for loops. You should only exit from a loop by a false loop condition—no exit or breaks allowed.
Two constructors: the default constructor and one constructor with two parameters,
Standard accessor and mutator methods.
greatestCommonFactor – This method should find the largest number that can be divided into the two numbers read from the file.
sumBetweenNumbers – This method should add all of the numbers between the two numbers read from the file (include the numbers read). For example: if the numbers were 5 and 9 then the method would add 5 + 6 + 7 + 8 + 9 to get 35.
Data Files:
Copy the data files from eCampus to your storage device.
Explanation of Data Files:
1 //this number tells you to use class OneNumber
27 // use this number to create a OneNumber object
1 //this number tells you to use class OneNumber
1000 //use this number to create a OneNumber object
2 //this number tells you to use class TwoNumber
300 //use this number and the next one to create a TwoNumber object
700
2 //this number tells you to use class TwoNumber
5 //use this number and the next one to create a TwoNumber object
9
… //and the file continues
I'd greatly appreciate any advice you can give me.
Also, here is my unfinished code.
The main program:
And here is my OneNumber class:Java Code:import java.util.Scanner; import java.io.*; import ch5.OneNumber; public class Chap5LabApp { public static void main(String[] args) throws IOException { //Variables OneNumber oneNum; TwoNumber twoNum; Scanner keyboard; int number; //get filenames for input and output files System.out.print("Enter input file's name: "); String infileName = keyboard.nextLine(); File file = new File(infileName); Scanner inputFile = new Scanner(file); System.out.print("Enter output file's name: "); String outfileName = keyboard.nextLine(); File file2 = new File(outfileName); Scanner outputFile = new Scanner(file2); //read input file while (inputFile.hasNext()) { } } }
Java Code:package ch5; public class OneNumber { //Field private int number; //Default Constructor public OneNumber() { number = 0; } //Parameterized constructor public OneNumber(int num) { number = num; } //Mutator and Acessor Methods public void setNumber(int num) { number = num; } public int getNumber() { return number; } //The isPrime method should determine if the number read from the file is a prime number. public void isPrime() { while ((number % number == 0) && (number % 1 == 0)) { System.out.println("The number is prime"); } } //The sumOfDigits method should find the sum of the digits of the number read from the file. public int sumOfDigits(int sum) { sum = 0; for(int counter = 0; counter < 100; counter++) { sum = sum+number; } return sum; } //The divisibleByNine method should determine if the number read from the file is divisible by 9. public void divisibleByNine() { while (number % 9 == 0) { System.out.println("The number is divisible by 9"); } } }
- 02-28-2012, 01:46 PM #2
Re: Need Help with Homework Lab Assignment on Loops and File Input/Output
I'll leave it to you to decide what your instructor wants.
If you read the data as a String then you can use the String class's methods to get at individual characters in the String.
For example the charAt() method will allow you to index through all the characters in the String.
One simple trick for converting a numeric char to its int value is to subtract '0' from it. For example: '7' - '0' = 7
The substring method would allow you to get any length of digit characters from the String.
- 02-28-2012, 02:12 PM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: Need Help with Homework Lab Assignment on Loops and File Input/Output
I can't make head nor tail of how you're supposed to figure out where one number starts and another ends.
What are you using to look at the file? Notepad?Please do not ask for code as refusal often offends.
- 02-28-2012, 04:05 PM #4
Member
- Join Date
- Feb 2012
- Posts
- 5
- Rep Power
- 0
Re: Need Help with Homework Lab Assignment on Loops and File Input/Output
Thank you Norm I shall consider using that although I am waiting for an email response from my instructor as well.
And yes Tolls I used Notepad to look at the file.
- 02-28-2012, 04:28 PM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: Need Help with Homework Lab Assignment on Loops and File Input/Output
Then I suspect there's line breaks you're not seeing, as Notepad does not recognise '\n' as a newline.
So I woudl start by opening the file in something other than Notepad, to make sure you can see the format.Please do not ask for code as refusal often offends.
- 02-29-2012, 07:49 AM #6
Member
- Join Date
- Feb 2012
- Posts
- 5
- Rep Power
- 0
Re: Need Help with Homework Lab Assignment on Loops and File Input/Output
Tolls, thanks for clarifying me that.
Ok so the instructor emailed me regarding the following:
She clarified that this method should break up a number into individual digits and then add up all the digits. Now how am I supposed to do this?--------------------------
sumOfDigits – This method should find the sum of the digits of the number read from the file. (For example: the number 123 would add 1 + 2 + 3 to get 6).
-----------------------------
- 02-29-2012, 10:15 AM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: Need Help with Homework Lab Assignment on Loops and File Input/Output
Have a look at the String API and see what method(s) might be applicable.
Please do not ask for code as refusal often offends.
- 02-29-2012, 01:15 PM #8
Similar Threads
-
Confused about File Input and Output Assignment
By rugger06 in forum New To JavaReplies: 2Last Post: 04-17-2011, 02:10 AM -
Input/ output file
By 3MAD in forum New To JavaReplies: 1Last Post: 04-15-2011, 12:59 PM -
File input output
By edcaru in forum New To JavaReplies: 5Last Post: 12-19-2010, 05:52 PM -
how to change the layout of an input file and write to an output file
By renu in forum New To JavaReplies: 8Last Post: 05-12-2010, 07:19 PM -
Calculator Program HELP NEEDED FAST! Homework assignment
By SteroidalPsycho in forum New To JavaReplies: 3Last Post: 03-05-2009, 04:02 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks