Lab Tasks 2
Lab Tasks 2
this.studentName = name;
this.regNo = regNo;
this.section = section;
this.contactNo = contactNo;
1|Page
UZAIR MALIK
[email protected]
“COMSATS UNIVERSITY ABBOTTABAD”
// Main method
student.displayStudentDetails();
Q 2 Time is an intangible concept. Analyze the concept and identify the data members and
methods that should be included in Time class.
2|Page
UZAIR MALIK
[email protected]
“COMSATS UNIVERSITY ABBOTTABAD”
this.hours = hours;
this.minutes = minutes;
this.seconds = seconds;
this.milliseconds = milliseconds;
time.displayTime();
Q 3 Car is an object that helps us in transportation. Analyze the concept and identify the data
members and methods that should be included in Car class
this.model = model;
this.color = color;
this.company = company;
this.engine = engine;
}
carDetails.displayCar();
4|Page
UZAIR MALIK
[email protected]
“COMSATS UNIVERSITY ABBOTTABAD”
Q 4..Rectangle is an object that represents a specific shape. Analyze the concept and identify the
data members and methods that should be included in Rectangle class.
*/
/**
*/
this.width=w;
this.height=h;
System.out.println("width:"+width);
System.out.println("height:"+height);
rect.display();
Q 5 Write an application that asks the user to enter two integers, obtains them from the user and
prints their sum, product, difference and quotient (division).
/*
*/
package asking.user;
import java.util.Scanner;
/**
*/
/**
*/
6|Page
UZAIR MALIK
[email protected]
“COMSATS UNIVERSITY ABBOTTABAD”
int n1=input.nextInt();
int n2=input.nextInt();
int sum=n1+n2;
int product=n1*n2;
System.out.println("there product is :"+product);
int substract=n2-n1;
double division=n1/n2;
Q 6 Write an application that reads an integer and determines and prints whether it’s odd or
even. [Hint: Use the remainder operator. An even number is a multiple of 2. Any multiple of 2
leaves remainder of 0 when divided by 2.]
import java.util.Scanner;
public class evenodd{
public static void main(String[] args)
{
Scanner input=new Scanner(System.in);
System.out.println("Enter a number: ");
int number=input.nextInt();
7|Page
UZAIR MALIK
[email protected]
“COMSATS UNIVERSITY ABBOTTABAD”
if(number%2==0)
{
System.out.println("the no you entered is even no g");
}
else
{
System.out.println("the no you entered is odd no g");
}
}
}
Q 7 Write a Java program to declare a double variable and then cast it to an int. Print both the
original and the casted values.
8|Page
UZAIR MALIK
[email protected]
“COMSATS UNIVERSITY ABBOTTABAD”
int intValue = 5;
// Addition
// Subtraction
// Multiplication
9|Page
UZAIR MALIK
[email protected]
“COMSATS UNIVERSITY ABBOTTABAD”
// Division
}
}
10 | P a g e
UZAIR MALIK
[email protected]