Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 16 of 16

Thread: Java solution required

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java solution required

    I need an urgent advise or solution to this:

    Problem Statement: Please write Java or C++ to create the following program: The nsg program should be a sequence generator program which can output the first N numbers of a given mathematical series to stdout. Your answer can contain a brief paragraph (less than 200 words) if needed. Your Please use object oriented programming techniques as applicable and specify any design patterns you are using.



    The program takes arguments as follows: argument 1 is a string corresponding to the mathematical series. At this stage, only 2 mathematical series are supported: “fibonacci” and “triangle”. Argument 2 needs to be a positive integer which specifies the number of values in the series to print out.



    Support for additional mathematical series will be needed in future versions of the program.



    Example invocations and outputs (the > signifies the command prompt)



    >nsg fibonnaci 5

    0,1,1,2,3



    >nsg triangle 3

    1,3,6



    >nsg prime 10

    prime is not a supported mathematical series



    >nsg triangle -1

    -1 is not a valid argument. You must specify a positive integer to print out numbers in the series.

    Thanks


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Java solution required

    First of all, The member introductions forum is not meant for posting questions/problems you have. It is specifically meant for new members to introduce themselves to the community.

    Secondly, this forum will not provide you with the solution to homework problems (either paid or unpaid). This is academic dishonesty, and the goal of the homework assignment of having you learn something ultimately was not met.

    However, if you provide us with what code you have so far and present the exact problem you're having (e.g. providing the compiler error if any), we'll be happy to help you solve your problem. If you're unsure where to start, I recommend taking a look at your class notes/textbook and The Java Tutorials from Sun/Oracle.

  3. #3
    Junior Member
    Join Date
    Feb 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java solution required

    Okay.I have written two separate classes for Prime Number and Fib series.Now,the requirement of the task is to put them in the same class using OO concepts.Need headsup on that please.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Java solution required

    put them in the same class
    Take the methods out of the two classes and put them into the new class. Same with class variables.

    Post the code you are having problems with and ask your questions.

  5. #5
    Junior Member
    Join Date
    Feb 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java solution required

    code for Fibonacci.java
    import java.io.*;
    class Fibonacci {
    public static void main(String args[]) {
    System.out.println("How many numbers of the sequence would you like?");
    InputStreamReader sr = new InputStreamReader(System.in);
        BufferedReader br    = new BufferedReader(sr);
     try {
          String input = br.readLine();
          int n = Integer.valueOf(input).intValue();
          fibonacci(n);
        } catch (NumberFormatException e){
          System.out.println("That is not an integer. Please enter an integer value");
        } catch (IOException e) {
          System.out.println("I did not recieve an input");
        }
      }
     
      public static void fibonacci(int n){
        int a=0,b=1;
     
        for (int i=0;i<n;i++){
          System.out.println(a);
          a=a+b;
          b=a-b;
        }
      }
    }
    code for prime.java
    class PrimeNumber {
      public static void main(String[] args) throws Exception{
      int i;
      BufferedReader bf = new BufferedReader(
      new InputStreamReader(System.in));
      System.out.println("Enter number:");
      int num = Integer.parseInt(bf.readLine());
      System.out.println("Prime number: ");
      for (i=1; i < num; i++ ){
      int j;
      for (j=2; j<i; j++){
      int n = i%j;
      if (n==0){
      break;
      }
      }
      if(i == j){
      System.out.print("  "+i);
      }
      }
      }
    }
    Last edited by helloworld922; February 4th, 2012 at 02:14 AM.

  6. #6
    Junior Member
    Join Date
    Feb 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java solution required

    My question is with two separate classes,how can I have my main class to handle both of them.How can I take a string argument to know I have to call Prime or Fibonacci class?

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Java solution required

    When posting code Please wrap your code with[code=java]<YOUR CODE HERE>[/code] to get highlighting.

    Do you know how to write a method? You should write methods that replaces the main() methods and then move those two methods to a new class.

    how can I have my main class to handle both of them.
    Once you have the two methods, the main method will be able to call either or both of them.

    How can I take a string argument to know I have to call Prime or Fibonacci class?
    That's your choice. Pick a String that chooses Prime and another that choose Fibonacci.
    Use an if statement to see which one was passed to the program and the call the desired method.

    If you put the selecting String on the command line following the name of the class, it will be placed in the main() method's String[] args array where you can get it.
    Last edited by Norm; February 3rd, 2012 at 08:45 PM.

  8. #8
    Junior Member
    Join Date
    Feb 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java solution required

    Can you please guide me how to declare an array of character to accept input for the series to be generated?

  9. #9
    Junior Member
    Join Date
    Feb 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java solution required

    Alright,I just need one suggestion.Need help taking char array argument for the type of series.Rest all set.My code is:
    import java.io.*;
     
    /*
     * Like Ruby, in java, everything is an object. To write a program, you'll need to
     * start by declaring a class. As in C, our program execution starts with the main
     * function. Also like C, Java is a compiled language, so you'll need to compile 
     * the code and then run the class using the java interpreter.
     */
     
    class Fibonacci {
     
      /*
       * So here we are defining the main function. Remember that this is supposed to
       * actually run this program, so the function needs to be `public`, in addition,
       * it's `static`, meaning we can call this method without an object of the Fibonacci
       * class being instantiated, and it doesn't need to return anything, as it will
       * run by the interpreter, which will handle the exit status. 
       */
     
      public static void main(String args[]) {
     
        /* 
         * We are using System.out.println here, but newer versions of Java have the printf method.
         */
     
        System.out.println("How many numbers of the sequence would you like?");
     
        /*
         * I'm sure there's more than one way to skin a cat, but to read stdin here, we
         * are creating a new BufferedReader, which will read one line of input.
         */
     
        InputStreamReader sr = new InputStreamReader(System.in);
        BufferedReader br    = new BufferedReader(sr);
        //BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
     
        /*
         * Now here is a concept we haven't addressed yet. The java compiler complains if
         * you try to call a method that could throw an exception (error), so I've included
         * an example here of how to handle the exceptions that could be thrown. Also, like
         * in our previous examples, we are casting the input to an integer.
         */
     
        try {
          String input = br.readLine();
          int num = Integer.valueOf(input).intValue();
     
          //String userInput = in.readLine();
          //Console console = System.console();
          //String username = console.readLine("User: ");
          if(num==5)
          fibonacci(num);
          else
          {
        	if(num<=0)
     
        	  System.out.println(" " + num + " is not a valid argument. You must specify a positive integer to print out numbers in the series");
          }
     
     
        } catch (NumberFormatException e){
          System.out.println("That is not an integer. Please enter an integer value");
        } catch (IOException e) {
          System.out.println("I did not recieve an input");
        }
      }
     
      /*
       * So here is our Fibonacci function. like the main function, it is public and can be
       * called without creating a Fibonacci object. We've also introduced a new method of 
       * calculating the sequence without using a temporary variable. In a later post, I will
       * examine the different algorithms used to calculate the Fibonacci sequence, and compare
       * performance in multiple languages.
       */
     
      public static void fibonacci(int num){
        int a=0,b=1;
     
        for (int i=0;i<num;i++){
          System.out.println(a);
          a=a+b;
          b=a-b;
        }
      }
      public static void prime(int num) {
      int i;
    	  for (i=1; i < num; i++ ){
    	  int j;
    	  for (j=2; j<i; j++){
    	  int n = i%j;
    	  if (n==0){
    	  break;
    	  }
    	  }
    	  if(i == j){
    	  System.out.print("  "+i);
    	  }
    	  }
    }
    }

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Java solution required

    Need help taking char array argument for the type of series
    Can you explain what you mean here?
    Why are you using a char array? Do you mean the String array passed to the main method?
    The contents of the array are from the command line. For example if you program were started with this line:
    java YourProgram firstArg secArg thirdArg

    Then args = {"firstArg", "secArg", "thirdArg"} an array with three elements and you'd use normal array indexing to get any one of the contents of the array.

  11. #11
    Junior Member
    Join Date
    Feb 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java solution required

    Hi,

    Well I want to take a string argument.Anyway,the problem got solved.Coming back to the original problem statement,is my code an appropriate solution? For original problem statement,refer to my first post in this thread? And my code is:
    import java.util.Scanner;
     
     
    class Series {
     
     
        public static void main(String[] args) {
     
    	Scanner input = new Scanner(System.in);
    	int num;
    	int num2;
     
     
    	//String word;
    	//String line;
     
    	System.out.print("Enter 1 for fibonacci and 2 for prime:  ");
    	num2 = input.nextInt();
    	if(num2>2 || num2<=0)
        {
        	System.out.println("" + num2 + ":this coded series is not a supported mathematical series");
        }
    	else
     
    	System.out.println("How many numbers of the sequence would you like?");	
    	num=input.nextInt();
     
     
     
     
            if(num2==1)
     
            	fibonacci(num);
            if(num2==2)
            	prime(num);
     
            if(num<0)
            	System.out.println(" " + num + " is not a valid argument. You must specify a positive integer to print out numbers in the series");
     
     
        }
        public static void fibonacci(int num){
            int a=0,b=1;
     
            for (int i=0;i<num;i++){
              System.out.println(a);
              a=a+b;
              b=a-b;
            }
          }
          public static void prime(int num) {
          int i;
        	  for (i=1; i < num; i++ ){
        	  int j;
        	  for (j=2; j<i; j++){
        	  int n = i%j;
        	  if (n==0){
        	  break;
        	  }
        	  }
        	  if(i == j){
        	  System.out.print("  " +i);
        	  }
        	  }
        }
    }

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Java solution required

    is my code an appropriate solution?
    Are you asking if I like your code
    or if your instructor will like your code?

    For example I don't like the way you align the {}s or the missing {}s with the if statements.
    Also your code is missing comments.

  13. #13
    Junior Member
    Join Date
    Feb 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java solution required

    What I am asking is if my solution is an appropriate Object Oriented approach to answering the required problem statement? I know my code lacks comments.

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Java solution required

    I don't care for the static methods.

  15. #15
    Junior Member
    Join Date
    Feb 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java solution required

    ???? Dint get.please elaborate

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Java solution required

    public static void

Similar Threads

  1. Replies: 1
    Last Post: November 6th, 2011, 09:48 PM
  2. New to Java....still learning...Help Required
    By muraduk in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 12th, 2010, 03:15 PM
  3. Java newbie...Help required
    By muraduk in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 12th, 2010, 03:15 PM
  4. Java Career Advice required
    By saads90 in forum The Cafe
    Replies: 1
    Last Post: April 26th, 2010, 08:59 AM
  5. Replies: 0
    Last Post: April 5th, 2010, 11:41 AM