Question
use java and write in text
a. Rewrite the following code segment using if statements. Assume that grade has been declared as of type char. char grade=
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer a: Code using if statement:

package javaProgram;

public class Main {
  
   public static void main(String []args) {
       char grade='B';
       if(grade=='A')
           System.out.println("Excellent");
       else if (grade=='B')
           System.out.println("Good");
       else
           System.out.println("you can do better");
   }
}
-------------------

screenshot:

e Console X <terminated> Main (5) Good Main.java X 1 package javaProgram; 2 3 public class Main { 4. 56 public static void ma

Answer b:

Code:

package javaProgram;

import java.util.*;

public class Main {
  
   public static void main(String []args) {
       String reverse="";
       String a="";
       Scanner sc=new Scanner(System.in);
       int n=sc.nextInt();
       a=Integer.toString(n%10);
       n=n/10;
       while(n!=0) {
           int x=n%10;
           a+="**"+x;
           n=n/10;
       }
       for(int i = a.length() - 1; i >= 0; i--)
       {
       reverse = reverse + a.charAt(i);
       }
       System.out.println(reverse);
   }
}

---------------------

screenshot:

2 Main.java X 1 package javaProgram; 2 3 import java.util.*; * 4 5 public class Main { 6 70 8 <terminated> 85 8**5 9 10 11 12

UMP 8 <terminated> 1234 1**2**3**4 Main.java X 1 package javaProgram; 2 3 import java.util.*; 4. 5 public class Main { 6 70 p

Answer c:

Code: Assuming that prime function already exist. Created the code and prime function also.

package javaProgram;

import java.util.*;

public class Main {
  
   static boolean isPrime(int n) {
       boolean flag=true;
       int m=n/2;
       if(n==0||n==1){
           flag=false;
       }
       else{
       for(int i=2;i<=m;i++){
           if(n%i==0){
           flag=false;   
           break;
           }
       }
       }
   return flag;
   }
  
   public static void main(String []args) {
       int[] arr = {1,3,5,9,1};
       int sumArray=0;
       for(int i=0;i<arr.length;i++) {
           if(isPrime(arr[i])==true) {
               sumArray+=arr[i];
           }
       }
       System.out.println("Sum of Prime number: "+sumArray);
   }
}

----------------------------------------------------------

screenshot:

e Console X XX B B E 5 8 <terminated> Main (5) [Java App Sum of Prime number: 8 Main.java x 6 70 static boolean isPrime(int n

Answer d:

Code:

package javaProgram;

import java.util.*;

public class Main {
  
   static boolean isSquare(double[][] arr_2D) {
       boolean flag=false;
       if(arr_2D.length==arr_2D[0].length) {
           flag=true;
       }
       return flag;
   }

  
   public static void main(String []args) {
       double arr_2D[][]= {{1,2,3},{1,4},{4,5,6}};
       if(isSquare(arr_2D)==true)
           System.out.println("The given array is Square Matrix");
      
   }
}

------------------------------------------

Screenshot:

0 Main.java X 1 package javaProgram; Console X <terminated> Main (5) [Java Application) C: The given array is Square Matrix 2

Answer e:

Code:

package javaProgram;

import java.util.*;

public class Main {
  
   static boolean isEqual(int[] A,int[] B) {
       boolean flag=false;
       if(A.length==B.length) {
           for(int i=0;i<A.length;i++) {
               if(A[i]==B[i])
                   flag=true;
           }
       }
       return flag;
   }

  
   public static void main(String []args) {
       int[] A= {1,2,3};
       int[] B= {1,2,3};
       if(isEqual(A,B)==true)
           System.out.println("Value in two array are equal");
       else
           System.out.println("Value in two array are not equal");
   }
}
----------------------------------

Screenshot of output:

U Console X X <terminated> Main (5) [Java Applicatio Value in two array are equal Main.java X 1 package javaProgram; 2 3 impo

If values of two arrays are not equal:

Main.java X 1 package javaProgram; 2 3 import java.util.*; Console X <terminated> Main (5) [Java Application) Value in two ar

Add a comment
Know the answer?
Add Answer to:
use java and write in text a. Rewrite the following code segment using if statements. Assume...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a function to reverse an integer using numeric operators and without using any arrays or...

    Write a function to reverse an integer using numeric operators and without using any arrays or other data structures. The signature of the function is: int f(int n) Examples if the input integer is return 1234 4321 12005 50021 1 1 1000 1 0 0 -12345 -54321 by java

  • write C code that uses pointers, arrays, and C strings. 3. Write a function called pow_xy....

    write C code that uses pointers, arrays, and C strings. 3. Write a function called pow_xy. The function should be passed 2 parameters, as illustrated in the prototype below. int pow_xy(int *xptr, int y); Assuming that xptr contains the address of variable x, pow_xy should compute x to the y power, and store the result as the new value of x. The function should also return the result. Do not use the built-in C function pow. For the remaining problems,...

  • What is the output of this code segment? double [] a = {-1.2, 3.1, -4.7, 38.0,...

    What is the output of this code segment? double [] a = {-1.2, 3.1, -4.7, 38.0, 0.0}; double temp = a[0]; for (int i = 1; i < a.length; i++) { if (a[i] < temp) { temp = a[i]; } } System.out.println (temp); What does this method do? In one short English sentence describe the task accomplished by this method. public static int foo(int [] a) { int temp = 0; for (int i = 0; i < a.length; i++)...

  • Note: According to the question, please write source code in java only using the class method....

    Note: According to the question, please write source code in java only using the class method. Sample Run (output) should be the same as displayed in the question below. Make sure the source code is working properly and no errors. Exercise #2: Design and implement a program (name it compareArrays) that compares the content of 2 single-dimensional arrays of the same size. The program prompts the users to enter the array size. Then prompts the user to initialize each array...

  • 3. Write Java methods to accomplish each of the following Write a method named simpleAry which...

    3. Write Java methods to accomplish each of the following Write a method named simpleAry which accepts and return nothing. It creates an array that can hold ten integers. Fill up each slot of the array with the number 113. Then, display the contents of the array on the screen. You must use a loop to put the values in the array and also to display them. Write a method named sury which accepts an array of type double with...

  • 1.Write code for a Java method, printArray, that takes an int array as parameter and prints...

    1.Write code for a Java method, printArray, that takes an int array as parameter and prints it out, one element per line. public static void printArray (int[] values){ Methods that do NOT return a result have “void” as return type. Notice how an array parameter type is passed, int [] }// end printArray 2.Write code for a Java method that takes in as input parameter an integer number, say num, and returns a double array of size num with all...

  • Language C Code Write a program that takes two integer arrays (A and B) and sums...

    Language C Code Write a program that takes two integer arrays (A and B) and sums them together (element wise). A third array to accept the result should be passed in as the output argument. Assume the arrays are all the same size. The argument N is the size of the arrays. Your code should provide a function with the following signature: void array Addition (int A[], int B[], int N, int output[]) { } Your code must also provide...

  • What is the java code for this. The goal of this lab is to write two...

    What is the java code for this. The goal of this lab is to write two programs, Summation and Prime, that execute simple tasks. The first computes the summation of integers within a range with a gap that the user specifies. The second tests whether the integer that the user enters is a square and if not, whether it is composite or prime. Summation Let us see some execution examples first, to get the sense of how the program works....

  • In Java programming language Please write code for the 6 methods below: Assume that Student class...

    In Java programming language Please write code for the 6 methods below: Assume that Student class has name, age, gpa, and major, constructor to initialize all data, method toString() to return string reresentation of student objects, getter methods to get age, major, and gpa, setter method to set age to given input, and method isHonors that returns boolean value true for honors students and false otherwise. 1) Write a method that accepts an array of student objects, and n, the...

  • Programming 5_1: Create a Java class named <YourName>5_1 In this class create a main method...

    Programming 5_1: Create a Java class named <YourName>5_1 In this class create a main method, but for now leave it empty. Then write a Java method getAverage which takes an array of integers as it’s argument. The method calculate the average of all the elements in the array, and returns that average. The method should work for an array of integers of any length greater than or equal to one. The method signature is shown below: public static double getAverage(...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT