Java Assignment

  1. Write a program to calculate the result of a mathematical expression. Read a mathematical expression from the user where the only operators allowed are addition, subtraction, multiplication and division, and the operands are all integers. The program will not consider operator precedence. Display the result of the expression. If the user enters an invalid expression (i.e. 2+5.6 or 2++5) display “Invalid Expression”

For Example:

Enter an expression: 2*10+5/5 Ans: 5

  1. Write a program that reads the data file “VoltageByCountry.csv” and displays the data in a table. The data consists of a header line and 3 columns separated by commas.

Hint: How to read from a file was covered in Lecture 4

Expected Output:

Table

Description automatically generated

3. Write a program that reads the data file “VoltageByCountry2.csv” and determines the most common voltage used in the world. Display the countries that use this voltage and the associated frequency.

For Example:

The most common voltage is 230V. The countries that use this voltage are:

– France @ 50hz – Germany @ 50hz – India @ 50Hz

– Ireland @ 50Hz
– Israel @ 50Hz
– Italy @ 50Hz
– New Zealand @ 50Hz – UK @ 50Hz

  1. A class called circle is designed as shown in the following class diagram. It contains:
    • Two private instance variables: radius (of the type double) and color (of the type String),

with default value of 1.0 and “red”, respectively.

  1. Two overloaded constructors – a default constructor with no argument, and a constructor

that takes a double argument for radius.
Two public methods: getRadius() and getArea(), which return the radius and area of this instance, respectively.

Create a new class to test the circle class

  1. Declare an instance of Circle class called c1
  2. Construct the instance c1 by invoking the “default” constructor, which sets its radius

and color to their default value.

  1. Output the message “The circle has radius of xx and area of yy”, where xx and yy are

replaced with the radius and area from the c1 object.

  1. Declare an instance of class circle called c2 by invoking the second constructor with a

radius obtained from the user (no validation needed) and default color.

  1. Output a similar message for c2 as was done for c1
  2. Write a program that read the file CNN.txt and stores it as a string. Asker the user to enter a query to search for. Display the number of times the query was found and the sentences in which the query occurs, with the query enclosed in square brackets. For Example:

  Enter query: dead

“dead” was found 2 times:

CNN affiliate YTN cited South Korean emergency response officials in reporting that six people were [dead]. The semiofficial Yonhap News Agency was still reporting four deaths early Thursday, mirroring what officials had told CNN on Wednesday.

“Can’t see a thing, it’s totally dark,” the message said. “We are not [dead] yet, so please send along this message.”