Write the given below problems using JAVA. Methods, Looping : Write a program th
ID: 3876112 • Letter: W
Question
Write the given below problems using JAVA.
Methods, Looping: Write a program that will allow the user to input a number. Then, print out all of the factors for the numbers. Include a run again option.
Arrays: Write a program that will generate a unique Fibonacci sequence. Allow the user to enter 3 numbers, add the numbers and subtract 13 to generate the next number. Fill an array of size 20 with these numbers. Then ask the user (using a GUI) which term they would like to see.
GUI, Text Files: Start by creating a text file with 5 digit id numbers followed by names. Read the data from the files into parallel arrays. Ask the user for an id number. If it matches, print out a welcome message of the name that goes with the id number.
11111
Bill Clinton
22222
Tyrion Lannister
….
Explanation / Answer
Methods, Looping
import java.util.Scanner;
public class FactorsFor {
private static Scanner sc;
public static void main(String[] args) {
int Number, i;
char ch;
sc = new Scanner(System.in);
do
{
System.out.println("Please Enter any number to Find Factors: ");
Number = sc.nextInt();
for(i = 1; i <= Number; i++) {
if(Number%i == 0) {
System.out.format(" %d ", i);
}
}
System.out.println(" Press Y/y to continue");
ch= sc.next().charAt(0);
}while(ch=='y' || ch=='Y');
}
}
2)
Arrays
import javax.swing.JOptionPane;
public class FibGUI
{
public static void main(String[] args)
{
int Number1,Number2, Number3,i,no,add;
int a[]=new int[20];
//JOptionPane.showMessageDialog(null,"Hello runner");
Number1 = Integer.parseInt(JOptionPane.showInputDialog("Enter First number"));
Number2 = Integer.parseInt(JOptionPane.showInputDialog("Enter Second number"));
Number3 = Integer.parseInt(JOptionPane.showInputDialog("Enter third number"));
add = Number1 + Number2 + Number3;
a[0]= Number1;
a[1]= Number2;
a[2]= Number3;
for(i=3;i<20;i++)
{
a[i] = a[i-1] + a[i-2] + a[i-3] - 13;
}
no = Integer.parseInt(JOptionPane.showInputDialog("Enter the term you want to display"));
JOptionPane.showMessageDialog(null, a[no+1]);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.