Assignment 1
Assignment 1
Prepared for
Rizwan Rashid
Prepared by
Name: Muhammad Zohaib
Roll No: SP22-BSE-042
2
Type of error:
In the above example the code is missing parenthesis. Because print statement require
( ) for its execution.
b) Write JAVA statements that can produce Logical Errors. Give three different examples
and briefly explain the reason (1-2 lines)
a)
public class Main{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int num1 = 4;
int num2 = 6;
int multi = num1 + num2;
System.out.print("the multiplication of num1 and num2 is " + multi);
}
}
Type of error:
In the example the error is we need multiplication of num1 and num2. But program is
performing addition of num1 and num2.
b)
public class Main{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int num1 = 5 + 4 * 3 / 2;
int num2 = (5 + 4) * 3 / 2;
int num3 = (5 + (4 * 3)) / 2;
int num4 = (5 + 4) * (3 / 2);
System.out.println("num1: " + num1);
System.out.println("num2: " + num2);
System.out.println("num3: " + num3);
System.out.println("num4: " + num4);
}
}
Type of error:
If we use incorrect operator precedence. It will change the output of code. In the above
code it produce 11 13 8 9 output.
c) Third
public class Main{
public static void main(String[] args){
4
c) Write JAVA statements that can produce Run Time Errors. Give three different examples and
briefly explain the reason (1-2 lines)
a)
public class Main{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int num1 = 10;
int num2 = 0;
int div = num1/num2;
System.out.println(div);
}
}
Type of error:
When we divide a number by 0 then java program produce a run time error. It will lead
to java.lang.ArithmeticException.
b)
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int num1 = input.nextInt();
System.out.println(num1);
}
}
Type of error:
5
If we input wrong data type in input that terminates the program abnormally. In the
above code the program terminate because it get input a string type instead of integer. It
show the error java.util.InputMismatchException.
c)
public class Main{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int num1 = 1000;
int num2 = (int)(Math.pow(num1, 12));
System.out.println(num2);
}
}
Type of error:
In the above code the error type is insufficient data type it will give answer to this power
which is 2147483647 which is the maxmum data that can store in the integer.
d) The following program has syntax errors. Write clearly type of error and its correction
(in tabular form). After you have corrected the syntax errors, show the output of this
program.
Question – 2: This question focuses on the basic elements of JAVA language (comments,
Special Symbols, Reserve Words and Identifiers)
Comments /*This program will calculate product of three
numbers */
// first number
Special symbols {}( ) “” //
Reserve words static
int
class
Identifiers main
String
args
num1
num2
num3
Standard input object variable that are defined in the code
Standard output object System.out.println("Product of numbers:
"+result);
7
C) Suppose a, b, and sum are int variables and c is a double variable. What
value is assigned to each variable after each statement executes? Suppose a
= 3 , b = 5 , and c = 14.1
8
statements a b c sum
sum = a + b + ( int) c; 3 5 14.1 22
c /= a; 3 5 4.7 22
b += (int) c - a; 3 6 4.7 22
a *= 2 * b + (int) c; 48 6 4.7 22
Question – 4:
Research several car-pooling websites. Create an application that calculates your daily driving
cost, so that you can estimate how much money could be saved by carpooling, which also has
other advantages such as reducing carbon emissions and reducing traffic congestion. The
application should input the following information and display the user’s cost per day of driving
to work:
a) Total miles driven per day.
b) Cost per gallon of gasoline.
c) Average miles per gallon.
d) Parking fees per day.
e) Tolls per day.
Code:
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.print("enter the total miles driven per day ");
double miles = input.nextDouble();
System.out.print("enter the cost galon of gasoline ");
double cost = input.nextDouble();
System.out.print("enter the average miles per galon ");
double average = input.nextDouble();
System.out.print("enter the parking fee per day ");
double parking = input.nextDouble();
System.out.print("enter the toll cost per day ");
double toll = input.nextDouble();
double galon = miles / average ;
double gasolinCost = cost * galon;
double total = gasolinCost + parking + toll;
System.out.printf("the total cost of user for driving to work is %.2f$", total);
}
}
output:
9
Question – 5:
Write an application that inputs one number consisting of five digits from the user and show the
number in reverse order. Note the reverse order must also be a number. E.g. 93324
Code:
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.print("enter the 5 digit number ");
int num, reverse, remainder;
reverse = 0;
num = input.nextInt();
while(num != 0){
remainder = num % 10;
reverse = reverse * 10 + remainder;
num = num / 10;
}
System.out.println(reverse);
}
}
output:
10