0% found this document useful (0 votes)
39 views10 pages

Assignment 1

This document contains an assignment for a Programming Fundamentals class. It asks students to write Java code that produces different types of errors, such as syntax errors, logical errors, and runtime errors. It also asks students to identify basic Java elements like comments, data types, operators, and input/output objects. Finally, it contains questions about primitive data types, expressions, and type conversion in Java code.

Uploaded by

Malik Zohaib
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views10 pages

Assignment 1

This document contains an assignment for a Programming Fundamentals class. It asks students to write Java code that produces different types of errors, such as syntax errors, logical errors, and runtime errors. It also asks students to identify basic Java elements like comments, data types, operators, and input/output objects. Finally, it contains questions about primitive data types, expressions, and type conversion in Java code.

Uploaded by

Malik Zohaib
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

1

COMSATS University Islamabad


Department of Computer Science
Programming Fundamentals (CSC103) – BSSE-2A
Class Assignment – 1 (CLO-1)

Prepared for
Rizwan Rashid

Prepared by
Name: Muhammad Zohaib
Roll No: SP22-BSE-042
2

Question – 1: This question focuses on the types of errors


a) Write JAVA statements that can produce Syntax Errors. Give three different examples
and write the names of errors .
a)
public class Main {
public static void main(String[] args){
int number = 2;
System.out.println(Number);
}
}
Type of error:
Java is a case sensitive language so number is different form Number. Using incorrect
name of variable in output statement.
b)
public class Main {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int number = input.nextInt();
System.out.println(number);
}
}
Type of error:
In this code we forget to import java class. In the above example I forget to import
Scanner class for getting input.
c)
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int number = input.nextInt();
System.out.print;
}
}
3

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

Scanner input = new Scanner(System.in);


int count;
for (count = 0; count<=10;count++);
{
System.out.println(count);
}
}
}
Type of error:
Misplace the semicolon can also change the output of code. In the above code we
require output in code but we are getting output outside of the loop.

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.

Wrong code Type of error Correction


count = 1 Cannot describe data type int count = 1;
PRIME Constant PRIME cannot initialize final int PRIME = 2;
sum = count + PRIME; Data type of sum is not given int sum = count + PRIME;
x := 25.67; Data type in not given and colon double x = 25.67;
is present in statement.
newNum = count * ONE + 2; Data type is not given and one int newNum = count * 1 +
should be given in digit 2;
sum + count = sum; Against the rule of java naming sum = count + sum;
convection.
x = x + sum * COUNT; The incorrect recall of veriable x = x + sum * count
count
System.out.println(" count = " Invalid recall of constant PRIME System.out.println(" count =
+ count + ", sum = " " + count + ", sum = "
+ sum + ", PRIME + sum + ",
= " + Prime); PRIME = " + PRIME);
Output:
6

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

Question – 3: This question focuses on the basic elements of JAVA language


(Primitive Data Types, Expressions and Assignments, Arithmetic Operators,
Order of Precedence, Augmented Assignment Operators, Type Conversion)
a)
public class Main{
public static void main(String[] args){
int x, y;
x = 10;
char ch ='B';
x = x + 5;
double payRate = 12.50;
int firstNum = 2;
int tempNum = firstNum;
y = 10;
int additional = x;
x = y;
y = additional;
double z = (double) (x + 12 / y -18);
System.out.println(z);
char grade = 'A';
int first, second, third, fourth;
first = 1;
second = 2;
third = 3;
fourth = 4;
x = (int)(z);
}
}
b) Suppose a, b and c are int variables and a = 5, b = 6, d = 2. What value is
assigned to each variable after each statement executes? If a variable is
undefined at a particular statement, report UND (undefined)
Statement a b c d
a = (b++) + 3 * ++d; 15 7 und 3
c = 2 * d + (++b) + a; 15 8 29 3
b = 2 * (++c) - (a++); 16 45 30 3
d = d++ + d + b++ + b; 16 46 30 98

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

You might also like