1. Problem 1 Design and implement an application that reads an arbitrary number
ID: 3532147 • Letter: 1
Question
1. Problem 1
Design and implement an application that reads an arbitrary number of integers that are in the range 0 to 50 inclusive and counts how many occurrences of each are entered. After all input has been processed, print all of the values (with the number of occurrences) that are entered one or more times. (Hint: can use idea from LetterCount program in the book)
2. Problem 3
Write a method named summit that accepts 2 integer arrays named gamma and delta. If the arrays are the same size the method will return an array of the sum of the corresponding elements. If they are of different sizes the method will return an array of 10 elements where all of the elements are set to -1. (Obviously, you need to put this method in a class and create an application to generate output.)
Create a class named ArrayMethods. No attributes. No Constructor (System will supply it). Implement the following methods which all take an integer array named list as a parameter:
3. The method named setup that will initialize all of the odd elements to -1 and set the even elements to their index number.
4. The method named fibo will initialize the array to the Fibonacci sequence. This sequence starts with 1 and 2 as the first 2 elements and each element thereafter is the sum of the previous two elements. (1, 2, 3, 5, 8, 13
Explanation / Answer
1.
import java.util.Scanner;
public class CountInts{
public static void main (String[] args){
int[] count = new int[51]; // new array indexed from 0 to 50
// initialized to contain all 0's
Scanner scan = new Scanner (System.in);
System.out.print("Enter integers, ending with <CONTROL> D: ");
while (scan.hasNext()){
int n = scan.nextInt();
if (n>=0 && n<=50)
count[n]++;
}
System.out.println(" Here are the counts of the integers"
+ " entered: ");
for (int i=0; i<=50; i++)
if (count[i]>0)
System.out.println(i + " " + count[i]);
}
}
2.
public class arr
{
public int[] compare(int a[], int b[])
{
if (a.length = b.length)
{
int []c = new int[a.length];
for(int i=0;i<a.length;i++)
{
c[i] = a[i] + b[i];
}
return c;
}
else
{
int c[] = {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1};
return c;
}
}
public static void main(String args[]) throws IOException
{
int gamma[] = {1,2,3,4,5,6,7,8,9,10};
int delta[] = {1,2,3,4,5,6,7,8,9,10};
int x[] = compare(gamma,delta);
for(int i=0;i<a.length;i++)
{
System.out.println(" " + x[i]);
}
}
}
4.
class fibonacci {
int fib(int num)
{
if(num==0)
{
return 0;
}
else if(num==1)
{
return 1;
}
else
return(fib(num-1)+fib(num-2));
}
public static void main(String arg[])
{
Scanner s=new Scanner(System.in);
System.out.println("Enter the value of n");
int n=s.nextInt();
fibonacci ob=new fibonacci();
for(int i=0;i<=n;i++)
{
System.out.println(ob.fib(i));
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.