In: Computer Science
projectOne (Please be sure to include screenshots of the test runs for all programs)
A.
write a program the computes nx and store the result into y
You can use y = Math.pow( Mantissa, exponent)
Requirements:
Besides main() your program must have one method with two
parameters, one double and one int
n and x are positive numbers read from the keyboard
if the user makes an entry that does not meet this criteria, the
user must be given to opportunity retry the entry
the user must be able to quit prior to entering values for n and x
Your method must compute y and return the result to its caller; the caller will print the result
After printing the result, the user must be queried again if there are other values to compute; if the answer is no exit the program, but if the user replies yes, then repeat the computation
B.
Write a Java program that takes an integer array parameter and
returns the sum of integers contained within the array.
C.
Correct code to output the folowing:
0 2 4 6 8 10 12 14 16 18
int[] numbers = new int[10];
for(int i=0; i < numbers.length; ++i)
numbers[i] = i * 2;
for(int i=0; i < numbers.length; i++)
System.out.print(numbers[++i] + " ");
System.out.println();
D.
Correct the code to prints the following:
0 1 2 3 4 5 6 7 8 9
int[] numbers = new int[10];
for(int i=0; i < numbers.length; ++i)
numbers[i] = i * 3;
for(int i=0; i < numbers.length; ++i)
System.out.print(numbers[i] / 2 + 1 + " ");
System.out.println();
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class power
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc= new Scanner(System.in);
System.out.print("Enter the mantissa : ");
int n = sc.nextInt();
System.out.print("\nEnter the exponent : ");
int x = sc.nextInt();
System.out.print("\nn^x is : " + Math.pow(n, x));
}
}
Code for part B
/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class sumArray
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc= new Scanner(System.in);
System.out.print("Enter the size of array : ");
int n = sc.nextInt();
int arr[] = new int[n];
int sum = 0;
for(int i = 0; i<n; i++)
{
System.out.print("\nEnter element #" + (i+1) + " : ");
int x = sc.nextInt();
arr[i] = x;
sum = sum + arr[i];
}
System.out.print("\nThe sum of all the elements is : " + sum);
}
}
int[] numbers = new int[10];
for(int i=0; i < numbers.length; ++i)
numbers[i] = i * 2;
for(int i=0; i < numbers.length; i++)
System.out.print(numbers[i] + " ");
System.out.println();
int[] numbers = new int[10];
for(int i=0; i < numbers.length; ++i)
numbers[i] = i * 3;
for(int i=0; i < numbers.length; ++i)
System.out.print((numbers[i] / 3) + " ");
System.out.println();
Please upvote the answer if you find it helpful.