• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help with program with classes.  RSS feed

 
brent bynum
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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. The data is numeric – do not use Strings.
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. The data is numeric – do not use Strings.
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 posted the assignment so you can hopefully help me by using it as a reference.

Main:

Not sure what to do now.





OneNumber class:

Did I do this class right?




TwoNumber class:

On this class I am not sure how to find the GCD. Also, not sure if my sumBetweenNumbers is correct since the numbers will be coming from a file.



 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don’t like that main method. In fact I don’t like any main methods with more than one statement in. Something like thisYou can have lots of main methods like that. They are very useful, because you can write NumberDemo classes at different stages of the development process and use them to test your app as far as you have got. You can use args, too. Invoke it from the command line with java NumberDemo 123 234 345 456 -999 and you can use those numbers (via Integer.parseInt(args[i])) in your classes. So you can test your app many times without having to write new files all the time.
Seeing your description, there are some things I would query.
  • 1: You cannot have a parameterised constructor and a default constructor in the same class. A default constructor is one created by the compiler if it doesn’t find any other constructors, so that is a contradiction in terms.
  • 2: I don’t like the notion of several constructors. If you have a constructor to set your value to a number, you ought not to have a constructor which sets the value to a default value, say 0.
  • 3: I don’t like the idea of mutator methods. Once you have set up the XNumbers class, let it be an immutable object, so its value never changes. I don’t think you will manage to swing that with your instructor, though.
  • Start off with a very simple OneNumber class, give it a toString() method, and set it offGradually add methods and you can add their result to the toString output.

    You have completely misunderstood the isPrime, dividesByNine and sumOfDigits methods. I think you need to go back to the drawing board with them.
    Did you know that if you substract the sum of digits from any natural number, you get a number exactly divisible by 9. (That applies to 0, too). So you can use the sum of numbers to determine whether the number divides by 9. I shall give you their headingsNote those return statements must be the last line in each method and you must not use return elsewhere in those methods.
    When you have got the OneNumber class working, try adding the TwoNumbers class.
     
    brent bynum
    Greenhorn
    Posts: 24
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    All your post did was confuse me...
     
    brent bynum
    Greenhorn
    Posts: 24
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I think I may have finished TwoNumber class. Is it right?

     
    Jeff Verdegan
    Bartender
    Posts: 6109
    6
    Android IntelliJ IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    All your post did was confuse me...


    In that case, I suggest you start fresh. Pick one aspect of the assignment that's giving you trouble. Start with a tiny program that just addresses that requirement and nothing else. Take your best shot at it, and post that plus a specific question if you get stuck.

    If you do it in small bits, one piece at a time, it will be easier for you to focus on the task at hand, and easier for us to understand what in particular is giving you trouble. Just posting the whole assignment and a wad of incomplete code and saying, "What do I do now?" is not an effective way to proceed.

    Good luck!
     
    Jeff Verdegan
    Bartender
    Posts: 6109
    6
    Android IntelliJ IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    brent bynum wrote:I think I may have finished TwoNumber class. Is it right?


    When you run various test data through it, does it behave as the instructions say it should? If not, then no, it's not right. If it passes the tests, and they were fair tests that covered relevant normal cases and corner cases, then it may be right. If you didn't do those tests, then you're not done yet.

    One thing that seems kind of odd to me is that at least one of your methods (greatestCommonFactor) changes the state of the object. That is, it modifies member variables. So once you run that method, you essentially have a different object, and if you run it twice in a row on the same object, you'll get two different results. I didn't notice anything in the instructions explicitly stating one way or the other about whether it's supposed to or allowed to do that, but normally a method like that would not modify the state of the object it's querying.
     
    Campbell Ritchie
    Marshal
    Posts: 56599
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Sorry to have confused you. Your isPrime method does not look to me as if it will tell whether your number is prime.
    I think you should google for Euclid’s algorithm, which is the oldest known way to work out a greatest common factor, and which can be implemented in Java in about 5 lines.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!