Results 1 to 8 of 8
  1. #1
    Terminus_Est is offline Member
    Join Date
    Feb 2012
    Posts
    5
    Rep Power
    0

    Exclamation 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.

    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
    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?

    I'd greatly appreciate any advice you can give me.

    Also, here is my unfinished code.

    The main program:
    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())
    		{
    			
    		}
    		
    
    	}
    
    }
    And here is my OneNumber class:

    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");
    		}
    			
    	}
    	
    	
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default 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.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,830
    Rep Power
    19

    Default 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.

    ** This space for rent **

  4. #4
    Terminus_Est is offline Member
    Join Date
    Feb 2012
    Posts
    5
    Rep Power
    0

    Default 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.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,830
    Rep Power
    19

    Default 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.

    ** This space for rent **

  6. #6
    Terminus_Est is offline Member
    Join Date
    Feb 2012
    Posts
    5
    Rep Power
    0

    Default 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:

    --------------------------
    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).
    -----------------------------
    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?

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,830
    Rep Power
    19

    Default 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.

    ** This space for rent **

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Need Help with Homework Lab Assignment on Loops and File Input/Output

    method should break up a number into individual digits
    How are the numbers represented in the program? Are they in a String variable or in a int?

Similar Threads

  1. Confused about File Input and Output Assignment
    By rugger06 in forum New To Java
    Replies: 2
    Last Post: 04-17-2011, 02:10 AM
  2. Input/ output file
    By 3MAD in forum New To Java
    Replies: 1
    Last Post: 04-15-2011, 12:59 PM
  3. File input output
    By edcaru in forum New To Java
    Replies: 5
    Last Post: 12-19-2010, 05:52 PM
  4. Replies: 8
    Last Post: 05-12-2010, 07:19 PM
  5. Calculator Program HELP NEEDED FAST! Homework assignment
    By SteroidalPsycho in forum New To Java
    Replies: 3
    Last Post: 03-05-2009, 04:02 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •