[jGrasp Java is used in my computer science class] 3. Write a complete, stand-al
ID: 3687039 • Letter: #
Question
[jGrasp Java is used in my computer science class]
3. Write a complete, stand-alone program in a single class called PrintSome, which works as follows: First, your program should include (copy and paste it in) this array of strings:
String[] names = {"Jo","Jim","Dana","Wilma","Lana","Ned","Nathaniel","Jill","Joe"};
Your program should read in a non-negative int value from the keyboard named numToPrint, and then print the first numToPrint names starting from the beginning of the array.
The names should be printed in a row, separated by blank spaces. Assume numToPrint is greater than or equal to 0 and less than or equal to 9.
For example: If numToPrint is 3 then only the following names in the following order should be printed in a row:
Jo Jim Dana
If numToPrint is 0 then no names are printed.
If numToPrint is 9 then all names are printed.
Answer:
4. Write a method called exchangewhich takes two parameters: an int variable index, and intArray, which is an array of type int.
The method returns an array of type int.
The method works as follows: the value stored at position index in intArray is exchanged with the value at the first position in intArray. It then returns this array.
Assume: 0 <= index < intArray.length
For example, on these inputs:
index = 3, intArray = {8, 6, 4, 5, 7}
The method would return this array:
5, 6, 4, 8, 7
Answer:
Explanation / Answer
Answer 3 -
import java.util.*;
import java.lang.*;
import java.io.*;
import java.util.Scanner;
import java.lang.String;
class Code
{
public static void main (String[] args) throws java.lang.Exception
{
String[] names = {"Jo","Jim","Dana","Wilma","Lana","Ned","Nathaniel","Jill","Joe"};
int numToPrint;
Scanner in = new Scanner(System.in);
System.out.println(" Enter numToPrint : ");
numToPrint = in.nextInt();
if(numToPrint>=0&&numToPrint<=9)
{
for(int i=0;i<numToPrint;i++)
{
System.out.print(names[i]+" ");
}
}
else
{
System.out.println(" Wrong Input");
}
}
}
Answer 4 -
import java.util.*;
import java.lang.*;
import java.io.*;
import java.util.Scanner;
import java.lang.String;
class Code
{
static int[] exchange(int index1,int [] arr1)
{
int temp=arr1[0];
arr1[0]=arr1[index1];
arr1[index1]=temp;
return arr1;
}
public static void main (String[] args) throws java.lang.Exception
{
int index,i,arr_size;
Scanner in = new Scanner(System.in);
System.out.println("Enter Array Length :");
arr_size = in.nextInt();
int [] arr=new int[arr_size];
for(i=0;i<arr_size;i++)
{
System.out.println("Enter Array Index "+i+" value :");
arr[i] = in.nextInt();
}
System.out.println("Enter index value to exchange : ");
index = in.nextInt();
if(index>=0&&index<arr_size)
{
arr=exchange(index,arr);
for(i=0;i<arr_size;i++)
{
System.out.println("Array index "+i+" value = "+arr[i]);
}
}
else
{
System.out.println("Wrong index value entered!!!");
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.