Download Homwork5 Solution

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
CS141 Programming Assignment #5
Due Wednesday, Nov 16th.
1) Write a class that asks the user for the day number (0 to 6) and prints the day name (Saturday to
Friday) using switch statement.
Solution 1:
/*Name: .......
* ID:.........
* assignment 5 Q1
*/
import java.util.Scanner;
public class Q1 {
public static void main(String arg[]){
Scanner input = new Scanner(System.in);
int dayNumber;
System.out.printf("Please, Enter the day number: \n");
dayNumber = input.nextInt();
switch (dayNumber) {
case 0:
System.out.printf("saturday");
break;
case 1:
System.out.printf("sunday");
break;
case 2:
System.out.printf("monday");
break;
case 3:
System.out.printf("tuesday");
break;
case 4:
System.out.printf(" wednesday");
break;
case 5:
System.out.printf("thursday");
break;
case 6:
System.out.printf("friday");
break;
default:
System.out.printf("Invalid value");
}//end of switch
}//end of main
}
2) Write a class that accepts user input from the console using switch. The class should take a
number and then test for the following age ranges: 0 to 10, 11 to 20, 21 to 30, 30 and over. Display a
message in the Output window in the following format:
user_age + " is between 21 and 30"
So if the user enters 27 as the age, the Output window should be this:
27 is between 21 and 30
If the user is 30 or over, you can just display the following message:
You are 30 or over
Solution 2:
/*Name: .......
* ID:.........
* assignment 5 Q2
*/
import java.util.Scanner;
public class Q2 {
public static void main(String[]args){
Scanner input = new Scanner (System.in);
int age;
System.out.println("please enter your age : ");
age =input.nextInt();
switch ((age-1)/10)
{
case 0: System.out.println(age +" is between 0 to 10 ");
break;
case 1: System.out.println(age +" is between 11 to 20 ");
break;
case 2: System.out.println(age +" is between 21 to 30 ");
break;
default : System.out.print("You are 30 or over");
}
}
}
3) Write a class that reads a string that is guaranteed to contain “or”. The program should construct
another string with “or” replaced by “and”. At end, the new string should be displayed on screen.
Example: “I will study Math or Biology” “I will study Math and Biology”
Solution 3:
/*Name: .......
* ID:.........
* assignment 5 Q3
*/
import java.util.Scanner ;
public class Q3 {
public static void main(String[] args) {
Scanner input= new Scanner(System.in);
System.out.printf("Please, Enter the text: \n");
String str1 = input.nextLine();
String str2=str1.replace("or","and");
System.out.println(str2);
} //end of main
}
4) Write a class that will read a string that is guaranteed to have 2 occurrences of the characters “er”.
The program should print the positions of these 2 occurrences. Here is an execution sample: (The
input is underlined)
Enter your sentence (it should contain 2 occurrences of “er”):
Ali is older than ahmed and taller also
The first occurrence of “er” is in position 10
The second occurrence of “er” is in position 32
Solution 4:
/*Name: .......
* ID:.........
* assignment 5 Q4
*/
import java.util.Scanner ;
public class Q4 {
public static void main(String[] args) {
Scanner input= new Scanner(System.in);
System.out.println("Enter your sentence (it should contain 2
occurrences of “er”):");
String str1 = input.nextLine();
int pos1 = str1.indexOf("er");
int pos2 = str1.indexOf("er",pos1 + 2);
System.out.printf("The first occurrence of “er” is in position %d
\n",pos1);
System.out.printf("The second occurrence of “er” is in position
%d \n",pos2);
} //end of main
}
5) Write a class to calculate the value of base exponent For example, if the user base is 3 and the user
exponent is 4 then calculates and print 34 (or 3 * 3 * 3 * 3). Assume that exponent is a positive,
nonzero integer and that base is an integer. Use for or while statement to control the calculation. Do
not use any Math class methods.
Solution 5:
/*Name: .......
* ID:.........
* assignment 5 Q5
*/
import java.util.Scanner;
public class Q5 {
public static void main(String[] args) {
int base, exp , result = 1 ;
Scanner input = new Scanner(System.in);
System.out.printf("Please, enter the value of the base : \n");
base =input.nextInt();
System.out.printf("Please, enter the value of the exponent : \n");
exp = input.nextInt();
for (int i = 0 ; i<exp ; i++ )
result*=base ;
System.out.printf("%d to the power %d = %d",base,exp,result);
}//end of main
}
6) Write a class that calculates the hypotenuse of a right triangle when the lengths of the other two
sides are given. The class should take two arguments of type double and print the hypotenuse as a
double.
Solution 6:
/*Name: .......
* ID:.........
* assignment 5 Q6
*/
import java.util.Scanner;
public class Q6 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double side1, side2, hyp , hypsqr;
System.out.print("Enter the value for Side 1: ");
side1 = input.nextDouble();
System.out.print("Enter the value for Side 2: ");
side2 = input.nextDouble();
hypsqr = Math.pow(side1, 2) + Math.pow(side2, 2) ;
hyp = Math.sqrt(hypsqr);
System.out.printf("The length of the hypotenuse is: %.2f",hyp);
}//end of main
}
7) Write a class to do the following :
a) prints the maximum integer in the array.
b) prints the minimum integer in the array.
c) prints the average of integers in the array.
d) print the array sorted in ASD and DES order.
e) print if array A and B are identical otherwise false
f) print the index of the first negative found or -1
Test your class you implemented with the following arrays
int [] a = {10 , 5 , -6 , 0 , 15 , -12, 20};
int [] b = {10 , 5 , -6 , 0 , 15 , -12};
Solution 7:
/*Name: .......
* ID:.........
* assignment 5 Q7
*/
public class Q7 {
public static void main(String[] args) {
int a[] = {10,5,-6,0,15,-12,20} ;
int b[] = {10,5,-6,0,15,-12} ;
System.out.println("Array a={10,5,-6,0,15,-12,20} \nArray b={10,5,
-6,0,15,-12}
");
/*find the min , max and avg of array a */
int min =0 , max = 0 , index = -1 ;
float sum = 0 ;
for(int i = 0 ;i<a.length ; i++){
if (i ==0 )
min = max =
a[0] ;
if(a[i]<min)
min = a[i];
if(a[i]>max)
max = a[i];
sum+=a[i];
if(a[i] < 0 && index ==-1)
index = i ;
}//end of for
System.out.printf("\nFor array a the min is : %d , the max is : %d
and the average is : %f \n",min ,max ,sum/a.length);
if (index>=0)
System.out.printf("\nThe index of the first negative number in
array a is %d \n",index);
else
System.out.println( index);
/* compare the arrays*/
if (a.length != b.length )
System.out.printf("\nThe two arrays a & b are not equal \n");
else {
boolean equal=true;
for (int i = 0; i < a.length; i++)
if (a[i]!=b[i])
equal=false;
if (equal)
System.out.println("\nThe two arrays are equal ");
else
System.out.println("\nThe two arrays are not equal ");
}
/* ASD ORDER of array a */
int t;
for (int i = 0; i < a.length; i++) {
for (int j = i + 1; j < a.length; j++) {
if (a[i] > a[j]) {
t = a[i];
a[i] = a[j];
a[j] = t;
}//end
if
}//end for
}//end for
System.out.println("Array a in ASD Order:");
for (int i = 0; i <a.length ; i++)
System.out.printf("%d ",a[i]);
System.out.print("\n\n");
/* DSD ORDER of array a*/
for (int i = 0; i < a.length; i++) {
for (int j = i + 1; j < a.length; j++) {
if (a[i] < a[j]) {
t = a[i];
a[i] = a[j];
a[j] = t;
}//end of if
}//end of j for
}//end of i for
System.out.printf("Array a in DSD Order: \n");
for (int i = 0; i <a.length ; i++)
System.out.printf("%d ",a[i]);
}//end of main
}