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

Lab Tasks 2

Uploaded by

malikuzair1242
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)
19 views10 pages

Lab Tasks 2

Uploaded by

malikuzair1242
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

“COMSATS UNIVERSITY ABBOTTABAD”

NAME : UZAIR MALIK


CLASS : 3B ,
REG.NO : SP23-BCS-065
TUTOR : MAM IQRA SARFRAZ
Q 1 A Student is an object in a university management System. Analyze the concept and identify
the data members that a Student class should have. Also analyze the behavior of student in a
university management System and identify the methods that should be included in Student
class.

public class StudentDetails {

// Data members declaration

public String studentName;

public int regNo;

public String section;

public String contactNo;

// Constructor to initialize data members

public StudentDetails(String name, int regNo, String section, String contactNo) {

this.studentName = name;

this.regNo = regNo;

this.section = section;

this.contactNo = contactNo;

// Method to display student details

public void displayStudentDetails() {

System.out.println("Name: " + studentName);

1|Page
UZAIR MALIK
[email protected]
“COMSATS UNIVERSITY ABBOTTABAD”

System.out.println("Registration Number: " + regNo);

System.out.println("Section: " + section);

System.out.println("Contact Number: " + contactNo);

// Main method

public static void main(String[] args) {

// Create an object of StudentDetails class

StudentDetails student = new StudentDetails("Uzair Malik", 12345, "A", "123-456-7890");

// Display student details

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.

public class Time {

// Data members declaration

private int hours;

private int minutes;

private int seconds;

private int milliseconds;

// Constructor to initialize data members

2|Page
UZAIR MALIK
[email protected]
“COMSATS UNIVERSITY ABBOTTABAD”

public Time(int hours, int minutes, int seconds, int milliseconds) {

this.hours = hours;

this.minutes = minutes;

this.seconds = seconds;

this.milliseconds = milliseconds;

// Method to display time

public void displayTime() {


System.out.println("Time: " + hours + ":" + minutes + ":" + seconds + "." + milliseconds);

public static void main(String[] args) {

// Create an object of Time class

Time time = new Time(1, 20, 30, 123);

// Display time details

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

public class Car {

public int model;

public String color;


3|Page
UZAIR MALIK
[email protected]
“COMSATS UNIVERSITY ABBOTTABAD”

public String company;

public int engine;

// Constructor to initialize data members

public Car(int model, String color, String company, int engine) {

this.model = model;

this.color = color;

this.company = company;

this.engine = engine;
}

// Method to display car details

public void displayCar() {

System.out.println("Car Model: " + model);

System.out.println("Car Color: " + color);

System.out.println("Company of the Car: " + company);

System.out.println("Engine Capacity: " + engine);

public static void main(String[] args) {

// Create an object of Car class

Car carDetails = new Car(2002, "red", "Suzuki", 700);

// Display car details

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.

*/

/**

* @author UZAIR MALIK

*/

public class Rectangle{

private int width;

private int height;

public Rectangle (int w,int h)

this.width=w;

this.height=h;

public void display()

System.out.println("width:"+width);

System.out.println("height:"+height);

public static void main(String[] args)


5|Page
UZAIR MALIK
[email protected]
“COMSATS UNIVERSITY ABBOTTABAD”

Rectangle rect=new Rectangle(12,3);

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).

/*

* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this


license

* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Main.java to edit this template

*/

package asking.user;

import java.util.Scanner;

/**

* @author UZAIR MALIK

*/

public class AskingUser {

/**

* @param args the command line arguments

*/

public static void main(String[] args) {

6|Page
UZAIR MALIK
[email protected]
“COMSATS UNIVERSITY ABBOTTABAD”

// TODO code application logic here

System.out.println("ENTER ANY TWO INTEGERS:");

Scanner input=new Scanner (System.in);

int n1=input.nextInt();

int n2=input.nextInt();

System.out.println("the two numbers entered by u are: "+ n1+ "\tand\t" +n2 );

int sum=n1+n2;

System.out.println("there sum is :"+sum);

int product=n1*n2;
System.out.println("there product is :"+product);

int substract=n2-n1;

System.out.println("there substraction will be "+substract);

double division=n1/n2;

System.out.println("there div is :"+division);

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.

public class implicit

public static void main(String[] args)

double originalDouble = 5.4;

int castedInt = (int) originalDouble;

System.out.println("Original double value: " + originalDouble);

System.out.println("Casted integer value: " + castedInt);

Q 8 Write a Java program to demonstrate type casting in arithmetic operations. Perform


arithmetic operations between int and double variables and cast the result to int and double.

public class TypeCastingDemo {

public static void main(String[] args) {

8|Page
UZAIR MALIK
[email protected]
“COMSATS UNIVERSITY ABBOTTABAD”

int intValue = 5;

double doubleValue = 2.5;

// Addition

double sum = intValue + doubleValue; // Automatic promotion to double

System.out.println("Sum (double): " + sum);

// Type casting the result to int

int sumAsInt = (int) (intValue + doubleValue);


System.out.println("Sum (int): " + sumAsInt);

// Subtraction

double difference = intValue - doubleValue; // Automatic promotion to double

System.out.println("Difference (double): " + difference);

// Type casting the result to int

int differenceAsInt = (int) (intValue - doubleValue);

System.out.println("Difference (int): " + differenceAsInt);

// Multiplication

double product = intValue * doubleValue; // Automatic promotion to double

System.out.println("Product (double): " + product);

// Type casting the result to int

int productAsInt = (int) (intValue * doubleValue);

System.out.println("Product (int): " + productAsInt);

9|Page
UZAIR MALIK
[email protected]
“COMSATS UNIVERSITY ABBOTTABAD”

// Division

double quotient = intValue / doubleValue; // Automatic promotion to double

System.out.println("Quotient (double): " + quotient);

// Type casting the result to int

int quotientAsInt = (int) (intValue / doubleValue);

System.out.println("Quotient (int): " + quotientAsInt);

}
}

10 | P a g e
UZAIR MALIK
[email protected]

You might also like