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.
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.
Re: Java solution required
Quote:
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.
Re: Java solution required
code for Fibonacci.java
code for prime.java
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?
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.
Quote:
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.
Quote:
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.
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?
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:
Code java:
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);
}
}
}
}
Re: Java solution required
Quote:
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.
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:
Code java:
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);
}
}
}
}
Re: Java solution required
Quote:
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.
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.
Re: Java solution required
I don't care for the static methods.
Re: Java solution required
???? Dint get.please elaborate
Re: Java solution required