1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
baherus [9]
3 years ago
10

Create a function void process(char ch, int x, int y)- to accept an arithmetic operator (+,-./, in argum

Computers and Technology
1 answer:
saul85 [17]3 years ago
6 0

Answer:

The program is as follows:

import java.util.*;

public class Main{

   public static void process(char ch, int x, int y){

if(ch == '+'){

    System.out.print(x+y);  }

else if(ch == '-'){

    System.out.print(x-y);  }

else if(ch == '*'){

    System.out.print(x*y);  }

else if(ch == '/'){

    if(y!=0){

        double num = x;

        System.out.print(num/y);      }

    else{

        System.out.print("Cannot divide by 0");     } }

else{

    System.out.print("Invalid operator"); }

 }

public static void main(String[] args) {

 Scanner input = new Scanner(System.in);

 int x, y;

 char ch;

 System.out.print("Enter two integers: ");

 x = input.nextInt(); y = input.nextInt();

 System.out.print("Enter operator: ");

 ch = input.next().charAt(0);  

 process(ch,x, y);

}

}

Explanation:

The function begins here

   public static void process(char ch, int x, int y){

If the character is +, this prints the sum of both integers

<em> if(ch == '+'){</em>

<em>     System.out.print(x+y);  }</em>

If the character is -, this prints the difference of both integers

<em> else if(ch == '-'){</em>

<em>     System.out.print(x-y);  }</em>

If the character is *, this prints the product of both integers

<em> else if(ch == '*'){</em>

<em>     System.out.print(x*y);  }</em>

If the character is /, this prints the division of both integers.

<em> else if(ch == '/'){</em>

<em>     if(y!=0){</em>

<em>         double num = x;</em>

<em>         System.out.print(num/y);      }</em>

<em>     else{</em>

<em>This is executed if the denominator is 0</em>

<em>         System.out.print("Cannot divide by 0");     } }</em>

Invalid operator is printed for every other character

<em>else{</em>

<em>     System.out.print("Invalid operator"); }</em>

<em> </em> }

The main begins here

public static void main(String[] args) {

 Scanner input = new Scanner(System.in);

This declares both integers

 int x, y;

This declares the operator

 char ch;

Get input for both integers

<em>  System.out.print("Enter two integers: ");</em>

<em>  x = input.nextInt(); y = input.nextInt();</em>

Get input for the operator

<em>  System.out.print("Enter operator: ");</em>

<em>  ch = input.next().charAt(0);   </em>

Call the function

 process(ch,x, y);

}

}

You might be interested in
Is the core of an operating system that controls its basic functions.
WINSTONCH [101]

Answer:

Explanation:

Tweaker

5 0
3 years ago
____ are types of changes that occur when text has been omitted from a document and must be inserted later.
VMariaS [17]
You already wrote the answer. It is Additions
7 0
3 years ago
Which of the following is the MOST important consideration when planning your budget?
Sladkaya [172]
C.budget for fixed expenses before flexiable expenses 

3 0
3 years ago
Read 2 more answers
How many searches should you complete when looking up information using online references?<br>​
antoniya [11.8K]
As many as it takes of course ;)
3 0
3 years ago
Ark titanoboa taming
ivann1987 [24]
Huh???????................
3 0
3 years ago
Other questions:
  • When using a wireless mouse, what is the most common port used for the transmitter? 
    7·1 answer
  • What are the advantages of using variables in a data type?
    7·1 answer
  • Explain the use of keyboard shortcuts and key combinations. You are at the tenth page of a 20-page document. You need to make ch
    14·1 answer
  • Describe five different ways databases can be processed
    14·1 answer
  • As defined by the National Institute of Standards and Technology​ (NIST), "________ is a model for enabling​ ubiquitous, conveni
    11·1 answer
  • Which of the following extends a network over a large geographic area by connecting local area networks together?
    14·2 answers
  • One of the driving forces in operating system evolution is advancement in the underlying hardware technology.
    8·1 answer
  • Write a partial class that shows a class constant and an instance method. Write an instance method that converts feet to inches
    9·1 answer
  • Will mark Brainliest, need help ASAP!
    12·1 answer
  • Which type of network allows backups and network security to be centrally located?
    11·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!