Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

// *************************************************************** // Calculator

ID: 3629702 • Letter: #

Question


// ***************************************************************
// Calculator.java
//
// Reads one of the arithmetic operators, +, -, *, /, or %
// and read two numbers to perform a selected arithmetic operation
// ***************************************************************


import java.util.Scanner;

public class Calculator
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);

System.out.println("Please choose an operation (+,-,*,/,%)");

String line = scan.next(); //a user enters one of +,-,*,/,%

//extract the character which will be one of +,-,*,/, or %
char operation = line.charAt(0);

System.out.println("Please enter an integer");
int firstNum = scan.nextInt();

System.out.println("Please enter another integer");
int secondNum = scan.nextInt();

int result = 0;

switch(operation)
{
case '+':
//Add the case where operation is ‘+’
//it should perform an addition using firstNum and secondNum
//and assign the added value to the variable “result”.



//complete other four cases for -, *, /, and %



default:
System.out.println("Invalid operation");

}

System.out.println(firstNum + " " + operation + " " + secondNum +
" = " + result);
}
}


For Loops

Count Vowels:

1. The program in CountVowels.java determines and prints how many of each vowel appear in the entire string.

1. Copy it to your directory
2. Add code for the step 1 (for loop) and step 2 (if statement)
3. Verify that your program is working correct

// ****************************************************************
// CountVowels.java
//
// Determines and prints how many of each vowel appear in the entire string.
// ****************************************************************

import java.util.Scanner;

public class CountVowels
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("Enter a string");
String line = scan.next();

//convert all characters to lower cases
line = line.toLowerCase();

//counter is used to count the number of vowels
int counter = 0;


//Step1: Complete the following for loop so that
//the index “i” goes from the beginning of the string to the end
//Hint: use length( ) method
for (int i = 0; ; )
{

//Step2: Complete the following if statement to check if
//character is one of vowels ‘a’,’e’,’i’,’o’, or ‘u’
//Hint: use charAt( ) method
if (line.charAt(i) == 'a' …………………..)
{
counter++;
}
}

System.out.println("The string " + line + " contains " + counter + " vowel(s)");
}
}

Explanation / Answer

Ok, your question is in two parts, (a) Calculator.java, (b) CountVowels.java.

So, for part (a):

Code:
import java.util.Scanner;

public class Calculator {

public static void main(String[] args) {

Scanner scan = new Scanner(System.in);

System.out.println("Please choose an operation (+,-,*,/,%)");

String line = scan.next(); // a user enters one of +,-,*,/,%

// extract the character which will be one of +,-,*,/, or %
char operation = line.charAt(0);

System.out.println("Please enter an integer");
int firstNum = scan.nextInt();

System.out.println("Please enter another integer");
int secondNum = scan.nextInt();

int result = 0;

switch (operation) {
case '+':
result = firstNum + secondNum;
break;
case '-':
result = firstNum - secondNum;
break;
case '*':
result = firstNum * secondNum;
break;
case '/':
result = firstNum / secondNum;
break;
case '%':
result = firstNum % secondNum;
break;
default:
System.out.println("Invalid operation");

}

System.out.println(firstNum + " " + operation + " " + secondNum + " = "
+ result);
}

}
-------------------------------------------------------

for part (b):
note: scan.next() will not read a complete line, just one word. Instead, use scan.nextLine()

Code:
import java.util.Scanner;

public class CountVowels {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter a string");
String line = scan.next(); // this will read just one word not complete line

//String line = scan.nextLine(); // this will read the complete line

// convert all characters to lower cases
line = line.toLowerCase();

// counter is used to count the number of vowels
int counter = 0;

// Step1: Complete the following for loop so that
// the index “i” goes from the beginning of the string to the end
// Hint: use length( ) method
for (int i = 0; i < line.length(); i++) {

// Step2: Complete the following if statement to check if
// character is one of vowels ‘a’,’e’,’i’,’o’, or ‘u’
// Hint: use charAt( ) method
if (line.charAt(i) == 'a') {
counter++;
}
if (line.charAt(i) == 'e') {
counter++;
}
if (line.charAt(i) == 'i') {
counter++;
}
if (line.charAt(i) == 'o') {
counter++;
}
if (line.charAt(i) == 'u') {
counter++;
}
}

System.out.println("The string '" + line + "' contains " + counter
+ " vowel(s).");
}
}