Descending Fibonacci numbers using an iterator - java

So my code essentially works, at least how I expected. But my issue is my Fibonacci numbers are supposed to be descending not ascending. The reason I am struggling with this is because the starting number is user input, so unlike ascending I don't know what the first step is everytime. I considered just loading the ints into an array, sorting and printing them, but I feel like that defeats the purpose of the iterator... Any thoughts? Here is my code:
import java.util.Iterator;
import java.util.Scanner;
import java.text.SimpleDateFormat;
import java.util.*;
import java.text.DateFormat;
public class Conversion1
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
int choice = 0;
int prime = 0, p = 0;
int fib = 0, f = 0;
long pTime = 0;
long fTime = 0;
long pETime = 0;
long fETime = 0;
int ff[];
long start = System.currentTimeMillis();
while (choice != 3)
{
System.out.println("Enter 1 or 2 to print out Fibonacci or Prime number iterator or enter 3 to exit.");
System.out.println("1 - Fibonacci Number Iterator");
System.out.println("2 - Prime Number Iterator");
System.out.println("3 - Exit");
choice = scanner.nextInt();
switch (choice)
{
case 1:
fTime += System.currentTimeMillis();
System.out.println("Enter the Max value");
int maxInt2 = scanner.nextInt();
ff = new int[maxInt2];
Iterator iterator = new FibonacciIterator(maxInt2);
while (iterator.hasNext())
{
ff[f] = (int) iterator.next();
f++;
}
for (int i = 0; i < f; i++)
System.out.println(ff[i]);
fETime += System.currentTimeMillis();
fib++;
break;
case 2:
pTime += System.currentTimeMillis();
System.out.println("Enter the Max value");
int maxInt = scanner.nextInt();
Iterator iterator2 = new PrimeIterator(maxInt);
while (iterator2.hasNext())
{
System.out.println(iterator2.next());
p++;
}
System.out.println("\n");
pETime += System.currentTimeMillis();
prime++;
break;
case 3:
long pFinal = pETime - pTime;
long fFinal = fETime - fTime;
double fseconds = ((fFinal / 1000) % 60);
double pseconds = ((pFinal / 1000) % 60);
System.out.println("\n");
System.out.println(fib + " Fibonacci commands yielding " + f + " individual outputs requiring "
+ fseconds + " seconds.");
System.out.println("\n");
System.out.println(prime + " Prime commands yielding " + p + " individual outputs requiring " + pseconds
+ " seconds.");
System.out.println("\n");
long end = System.currentTimeMillis();
DateFormat df = new SimpleDateFormat("HH':'mm':'ss");
System.out.println("Program started at " + df.format(new Date(start)) + " and terminated at: "
+ df.format(new Date(end)));
System.out.println("\n");
System.out.println("Program Ended");
System.out.println("\n");
break;
default:
System.out.println("Invalid Input");
}
}
}
static class PrimeIterator implements java.util.Iterator
{
private int limit = 0;
private int current;
private int a_number;
public PrimeIterator(int current)
{
this.current = current;
}
#Override
public Integer next()
{
return current;
}
static boolean isPrime(int number)
{
for (int divisor = 2; divisor < number; divisor++)
if (number % divisor == 0)
return false;
return true;
}
#Override
public boolean hasNext()
{
current--;
while (true)
{
if (isPrime(current))
break;
current--;
}
if (current <= limit)
return false;
else
return true;
}
}
static class FibonacciIterator implements java.util.Iterator
{
private int limit;
private int current = 1;// -1,1,0,1,1,2,3,5
private int prev = -1;
public FibonacciIterator(int limit)
{
this.limit = limit;
}
#Override
public Integer next()
{
return current;
}
#Override
public boolean hasNext()
{
int temp = current;
current = current + prev;// -1+1=0
prev = temp;
if (current >= limit)
return false;
else
return true;
}
#Override
public void remove()
{
throw new UnsupportedOperationException("Method not supported");
}
}
}

If you have to reverse the order, can you not just flip the order of the Fibonacci values captured?
for (int i = f - 1; i >= 0; i--)
System.out.println(ff[i]);
As opposed to what you have where you start at index 0.
for (int i = 0; i < f; i++)
System.out.println(ff[i]);

Related

Can't get results from boolean to appear correctly

My program asks the user for a number and then decides if the number is between the range of two randomly generated numbers or outside of it. Everything works fine, except the program keeps giving the result that the guessed number is outside the range, even when it is inside the range. Not sure how to get the answer to show correctly. Boolean result = true is there since a "Cannot find symbol" error appears if it is not.
Code:
public static int getValidGuess(Scanner get)
{
int num;
System.out.print("Guess a number: --> ");
num = get.nextInt();
return num;
} // getValidGuess end
public static boolean displayGuessResults(int start, int end, int num)
{
int n1, n2;
boolean result = true;
Random gen = new Random();
n1 = gen.nextInt(99) + 1;
n2 = gen.nextInt(99) + 1;
if(n1 < n2)
{
start = n1;
end = n2;
} // if end
else
{
start = n2;
end = n1;
} //else end
if(num > start && num < end){
result = true;
System.out.println("\nThe 2 random numbers are " + start +
" and " + end);
System.out.println("Good Guess!");
} //if end
if(num < start || num > end){
result = false;
System.out.println("\nThe 2 random numbers are " + start +
" and " + end);
System.out.println("Outside range.");
} //if end
return result;
} // displayGuessResults end
public static void main(String[] args) {
// start code here
int start = 0, end = 0, num = 0, input;
Scanner scan = new Scanner(System.in);
String doAgain = "Yes";
while (doAgain.equalsIgnoreCase("YES")) {
// call method
input = getValidGuess(scan);
displayGuessResults(start, end, num);
System.out.print("\nEnter YES to repeat --> ");
doAgain = scan.next();
} //end while loop
} //main end
Your displayGuessResult should be improved:
public static boolean displayGuessResults(int num) {
boolean result = true;
Random gen = new Random();
int n1 = gen.nextInt(99) + 1;
int n2 = gen.nextInt(99) + 1;
int start = Math.min(n1, n2);
int end = Math.max(n1, n2);
System.out.println("\nThe 2 random numbers are " + start + " and " + end);
if(num >= start && num <= end){
result = true;
System.out.println("Good Guess!");
} else {
result = false;
System.out.println("Outside range.");
}
return result;
} // displayGuessResults end
and you have to call it using input read from the Scanner:
input = getValidGuess(scan);
displayGuessResults(input);

How to find factorial and show result of counting in console?

public class Car {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
System.out.println(n+"!="+factorial(n));
}
public static int factorial(int num) {
return (num == 0) ? 1 : num * factorial (num - 1);
}
}
how make this code to text in console 3! = 1*2*3 = 6?
Don't use recursion for this. Besides, it isn't really efficient or necessary.
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int fact = 1;
String s = n + "! = 1";
for (int i = 2; i <= n; i++) {
fact *= i;
s += "*" + i;
}
s += " = ";
System.out.println(s + fact);
There can be many ways to do it e.g. you can build the required string or print the trail while calculating the factorial. In the following example, I have done the former.
As an aside, you should check the input whether it is a positive integer.
import java.util.Scanner;
public class Car {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter a positive integer: ");
int n = in.nextInt();
if (n >= 0) {
StringBuilder strFact = new StringBuilder();
int fact = factorial(n, strFact);
if (strFact.length() > 0) {
// Delete the last '*'
strFact.deleteCharAt(strFact.length() - 1);
System.out.println(n + "!= " + strFact + " = " + fact);
} else {
System.out.println(n + "!= " + fact);
}
} else {
System.out.println("This is an invalid input.");
}
}
public static int factorial(int num, StringBuilder strFact) {
int fact;
if (num == 0) {
fact = 1;
} else {
fact = num * factorial(num - 1, strFact);
strFact.append(num + "*");
}
return fact;
}
}
A sample run:
Enter an integer: 3
3!= 1*2*3 = 6

Java: How to use binary search with an array

For this java program, I'm trying to use a binary search for the array I have created in the Class Numbers, however, when I enter the 4th choice, the program just ends. The method binSearch is in Class Numbers. I can enter the required the size of the array, generate random numbers, search for specific numbers, and display them. However, when I want to use a binary search, the program ends as previously said. What is the reason why the program ends and what needs to be done to fix that certain method?
public class Numbers {
int[] array;
private int sizeOfArray;
public Numbers() {
sizeOfArray = 0;
array= new int [sizeOfArray];
}
public Numbers(int sizeOfArray) {
this.sizeOfArray = sizeOfArray;
array= new int [sizeOfArray];
}
public void generateNumbers() {
Random randomNumber = new Random();
int theNumber = 0;
for (int i = 0; i < sizeOfArray; i++) {
theNumber = randomNumber.nextInt(50);
array[i] = theNumber;
}
}
public int count(int num) {
int theNumbers = 0;
for (int i = 0; i < sizeOfArray; i++) {
if (array[i] == num) {
theNumbers++;
}
}
return theNumbers;
} // end count method
public String toString() {
String myArray = "";
for (int i = 0; i < sizeOfArray; i++) {
myArray += array[i] + " ";
}
return myArray;
}
public int binSearch(int[] array, int key) {
int low = 0;
int high = sizeOfArray - 1;
//int middle = (low + high + 1) /2;
int location = -1;
while (high >= low) {
int middle1 = (low + high) / 2;
if (array[middle1] == key ) {
//return true;
}
if (array[middle1] < key) {
low = middle1 + 1;
}
if (array[middle1] > key) {
high = middle1 - 1;
}
}
//return false;
return location;
}
}
and here is the main menu:
boolean isDone = false;
String input = null;
Numbers theNumber = new Numbers();
Scanner scanner = new Scanner (System.in);
try {
while (isDone == false) {
/*Menu options */
System.out.println("Enter 1 to create array size");
System.out.println("Enter 2 to generate random numbers");
System.out.println("Enter 3 to search and display number of occurrences");
System.out.println("Enter 4 to binary search to find whether specific number exists");
System.out.println("Enter 5 to display the array");
System.out.println("Enter 6 to quit the program");
input = scanner.nextLine();
switch (input) {
case "1":
int intNumber1 = 0;
System.out.println("Enter required size:");
intNumber1 = Integer.valueOf(scanner.nextLine());
theNumber = new Numbers(intNumber1);
System.out.println("Array has been generated.");
break;
case "2":
//theNumber = new Numbers();
theNumber.generateNumbers();
System.out.println("Numbers have been generated and stored.");
break;
case "3":
int intNumber2 = 0;
System.out.println("Enter number to search for: ");
intNumber2 = Integer.valueOf(scanner.nextLine());
System.out.println("Number of occurences of " + intNumber2 + " in the array is " + theNumber.count(intNumber2) + ".");
break;
case "4":
int key = 0;
theNumber.binSearch(null, key);
System.out.println("Array is sorted: ");
break;
case "5":
int theNumbers = 0;
if (theNumbers == 0)
{
System.out.println("No array has not been generated yet.");
}
else
{
System.out.println("The numbers are: ");
}
System.out.println(theNumber.toString());
break;
case "6":
isDone = true;
System.out.println("Bye... See you again");
scanner.close();
break;
default:
System.out.println("These are invalid choices...please reenter.");
break;
}
}
}
catch (Exception exception)
{
System.out.println("This is an invalid choice...please reenter.");
scanner.nextLine();
return;
}

How Do I Make this For Loop continue after the Thread finishes?

Thread used
public class MissedThread extends Thread
{
public synchronized void run()
{
try
{
Thread.sleep(1000);
System.out.println("Too slow");
}catch(InterruptedException e){return;}
}
}
Program that uses aforementioned Thread
import java.util.Scanner;
public class FastMath
{
public static void main(String[] args)
{
System.out.println("How many questions can you solve?");
Scanner in = new Scanner(System.in);
int total = in.nextInt();
MissedThread m = new MissedThread();
int right = 0;
int wrong = 0;
int missed = 0;
for(int i = 0;i<total;i++)
{
int n1 = (int)(Math.random()*12)+1;
int n2 = (int)(Math.random()*12)+1;
System.out.print(n1+" * "+n2+" = ");
m.start();
int answer = in.nextInt();
if(answer==n1*n2)
{
right++;
continue;
}
if(answer!=n1*n2)
{
wrong++;
continue;
}
}
}
}
So the purpose of the program is that if the user does not enter a number within 1 second (duration of the Thread.sleep), it will print a message and continue onto the next iteration. However instead if it's answered in time, it will just stop the program. And if it's not answered in time, it seems to get stuck and not move to the next iteration of the for-loop.
You don't need to wait for the answer from another thread. This is how it could be done using a single thread:
public class FastMath {
public static void main(String[] args) throws IOException {
int answer;
System.out.println("How many questions can you solve?");
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int total = Integer.valueOf(in.readLine());
int right = 0;
int wrong = 0;
int missed = 0;
for (int i = 0; i < total; i++) {
int n1 = (int) (Math.random() * 12) + 1;
int n2 = (int) (Math.random() * 12) + 1;
System.out.print(n1 + " * " + n2 + " = ");
long startTime = System.currentTimeMillis();
while ((System.currentTimeMillis() - startTime) < 3 * 1000
&& !in.ready()) {
}
if (in.ready()) {
answer = Integer.valueOf(in.readLine());
if (answer == n1 * n2)
right++;
else
wrong++;
} else {
missed++;
System.out.println("Time's up!");
}
}
System.out.printf("Results:\n\tCorrect answers: %d\n\nWrong answers:%d\n\tMissed answers:%d\n", right, wrong, missed);
}
}

Java skipping a number in the sequence

This is very interesting, i notice. Before i can explain further its best i show the code and you will understand what i mean.
This is the code:
public class Qn3 {
static BigDecimal[] accbal = new BigDecimal[19];
private static Integer[] accnums = new Integer[19];
public static void main(String[] args) {
addaccount();
}
public static void addAccount() {
int i = 0, accno, input, j, check;
BigDecimal accbala;
DecimalFormat df = new DecimalFormat("0.00");
Scanner sc = new Scanner(System.in);
Scanner in = new Scanner(System.in);
accnums[1] = new Integer(1);
while (accnums.length >= count(accnums)) {
System.out.print("Enter the account number: ");
while (sc.hasNext("[0-9]{7}")) {
accno = sc.nextInt();
System.out.print("Enter account balance: ");
accbala = in.nextBigDecimal();
for (j = 0; j < accnums.length; j++) {
if (accnums[j] == null)
break;
else if (accnums[j].equals(accno)) {
break;
}
}
if (j == accnums.length) {
System.out.print("No more than 20 accounts can be added.");
} else if (accnums[j] != null) {
if ((accnums[j].equals(accno)))
System.out.println("Account already exists");
break;
} else {
accnums[j] = accno;
accbala = accbala.setScale(2, RoundingMode.HALF_UP);
accbal[j] = accbala;
check = j;
System.out.println("Current number of accounts in the system: "
+ (check + 1)
+ "\nNumber of accounts still can be added: "
+ (20 - (check + 1)));
}
}
while (!sc.hasNext("[0-9]{7}")) {
System.out.println("Wrong NRIC");
break;
}
while (accnums.length <= count(accnums)) {
System.out.println("20 accounts have already been created");
break;
}
break;
}
}
private static int count(Integer[] array) {
int count = 0;
// accnums = new Integer[] {1,2};
for (int index = 0; index < array.length; index++) {
if (array[index] != null) {
count++;
}
}
// System.out.println("You have used " + count + " slots");
return count;
}
}
So now that you have seen the code the problem that is hard to notice is this, take note of the line in the addaccount() method where
System.out.println("Current number of accounts in the system: "+(check+1)+"\nNumber of accounts still can be added: "+(20 - (check+1)));
this line the first check+1 will give me 1 then the next one gives me 3! and then the next time i run the method it gives me 4 and then again 5 and so on so forth, what is happening to 2?
You have that println in an else block, and when j == 1 you're hitting the else if case. Try removing this line
accnums[1] = new Integer (1);

Categories