Assignment
Q1. Explain what each of the two program segments computes:
1- int x = 2;
int y = x + x;
2- String s = "2";
String t = s + s;
Q2. Write the following mathematical expressions in Java.
dm = m (√(1+ v/c)/√(1 - v/c) - 1) Volume = 4Πr2/3
Q3. What errors does this class have? How would you fix them? Rewrite the correct version of this class.
public class Square {
private double sideLength;
private double area;
publicSquare(double sideLength) {
sideLength = sideLength;
area = sideLength * sideLength;
}
public double getArea() {
returnarea;
}
public static void main(string[] args){
Square sq = new Square(4.4);
System.out.print("This Area of the square is:\t +sq. getArea);
Q4. What is the order of execution of operators in the following equation?
a=99*5+4%2/6+(18-4)*2
Q5. Write a program called CheckPassFail which read the mark from user and prints "PASS" if the mark is more than or equal to 60; or prints "FAIL" otherwise.
Q6. Write a program that that computes the maximum of two numbers. You need to follow the following instructions:
1. Create a class called "MaxProgram'.
2. The class MaxProgram has only one methodcalled "maxValue" . This methodtakestwointegernumbers as parameters. The methodreturns the maximum of the twonumbers, elseitreturns -1, If bothnumbers are equal.
1. To test this class, createanother class called "Tester", with a main method.
In the main method, create an object of the "MaxProgram" class.
2. From the createdobject, call the "maxValue" method, and giveitanytwonumbers.
3. Print the returned maximum number.
Q7. A company applies a discount based on the price of the order. If the order is over $50, the discount is 5%. If the order is over $100, the discount is 10%. Otherwise, there is no discount. Write a sequence of if statements to get the discounted price of the order.