Assignment 2 Due date Thursday November17th 2016at 1159 PM
Assignment 2
Due date: Thursday - November17th, 2016at 11:59 PM.
--------------------------------------------------------------------------------------------------------
Q1. Explain what each of the following 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.
--------------------------------------------------------------------------------------------------------
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;
}
public double getArea() {
area = sideLength * sideLength;
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.
Solution
Q1)
1. Answer: y = 4 Adding two integers results in summation
2. Answer: t = 22 Adding two strings means concatenating them
Q2)
dm = m * ((Math.sqrt(1+(v/c)) / Math.sqrt(1-(v/c)))-1)
volume = (4 * (22/7) * Math.pow(r,3))/3
Q3)
Line 4: Space needed between public and square
Line 9: Space between return and area
Line 10: Capital S needed in string
Line 12: getArea to be replaced as getArea() because it is a method call
doing all these corrections will clear all the syntax errors and gives the output as 0.0.
The reason is the variable area in getArea() method is calculated taking global variable sideLength as operand. By default the global variable is assigned to zero. In the constructor sideLength = sideLength statement does not initialize the new value passed to be assigned to global variable. It has to be replaced by this.sideLength = sideLength.
The corrected version of the program is
public class Square
{
private double sideLength;
private double area;
public Square(double sideLength)
{
this.sideLength = sideLength;
}
public double getArea()
{
area = sideLength * sideLength;
return area;
}
public static void main(String[] args)
{
Square sq = new Square(4.4);
System.out.print(\"This Area of the square is:\\t\" +sq.getArea());
}
}
Q5)
import java.util.*;
class CheckPassFail
{
public static void main(String args[])
{
Scanner s = new Scanner(System.in);
System.out.println(\"Enter the marks\");
int marks = s.nextInt();
if(marks>60)
System.out.println(\"PASS\");
else
System.out.println(\"PASS\");
}
}


