0% found this document useful (0 votes)
38 views5 pages

Lab 2 BSCS 13C OOP QUESTIONS

This document provides details for Lab 2 of the CS 212 Object Oriented Programming class. The lab aims to help students understand Java data types and operators, which are fundamental concepts. It lists 6 tasks for students to complete that involve declaring and using different data types, performing operations, and writing small programs to work with data types and operators. The tasks include storing student information, separating digit input, calculating position with physics equations, printing tables, checking palindromes, converting binary to decimal, and calculating factorials. Completing the tasks will help students gain a strong foundation for more advanced Java concepts.

Uploaded by

tayyabrockstar1
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)
38 views5 pages

Lab 2 BSCS 13C OOP QUESTIONS

This document provides details for Lab 2 of the CS 212 Object Oriented Programming class. The lab aims to help students understand Java data types and operators, which are fundamental concepts. It lists 6 tasks for students to complete that involve declaring and using different data types, performing operations, and writing small programs to work with data types and operators. The tasks include storing student information, separating digit input, calculating position with physics equations, printing tables, checking palindromes, converting binary to decimal, and calculating factorials. Completing the tasks will help students gain a strong foundation for more advanced Java concepts.

Uploaded by

tayyabrockstar1
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/ 5

Department of Computing

CS 212: Object Oriented Programming


Class: BSCS-13C
Lab 2: Java Basics [Data Types and Operators]
Date: 15-02-2024
Time: 2:00pm- 5:00 pm
Instructor: Mr. Jaudat Mamoon
Lab Engineer: Mr. Danyal Sadiq

CS 212: Object Oriented Programming Page 1


Lab 02: Java Basics [Data Types and Operators]
Aim: In this lab, you will delve into the foundational aspects of Java programming, specifically focusing
on data types and operators. Understanding these fundamental concepts is crucial as they form the
building blocks for more complex programming constructs in Java.

Objective: The primary objective of this lab is to provide you with a solid understanding of the following
key concepts:

 Different data types available in Java and their usage.


 Various operators supported by Java for performing operations on data.

Tools/ Software: VS Code, NetBeans, Eclipse etc.

Deliverables: Submit a single file on LMS before the due date as communicated by Lab Engineer. Please
attempt the lab quiz at the defined time.

Note: Write your registration number and name at the top of each code. You will submit your working
codes and outputs (Just Screenshots) in the Lab Report.

Data Types and Operators in Java

Data Types: In Java, data types are classifications used to categorize various types of data that can be
used in a program. Java supports both primitive data types and reference data types.

1. Primitive Data Types: Java provides several primitive data types, including:
 int: Used to represent integer values.
 double: Used to represent floating-point numbers (decimal values).
 char: Used to represent a single character.
 boolean: Used to represent true or false values.

Each primitive data type has specific size and range constraints, which determine the range of values it
can hold. For example, an int typically holds values from -2^31 to 2^31 - 1.

2. Reference Data Types: In addition to primitive data types, Java also supports reference data
types. Reference data types include arrays and strings, which are used to handle more complex
data structures. Arrays allow you to store multiple values of the same type in a single variable,
while strings represent sequences of characters.

Operators: Operators in Java are symbols that perform operations on operands. Java offers a wide range
of operators for various types of operations, including arithmetic, relational, logical, bitwise, and
assignment operations.

CS 212: Object Oriented Programming Page 2


1. Arithmetic Operators: Arithmetic operators perform basic mathematical computations on
numeric operands. Examples include addition (+), subtraction (-), multiplication (*), division (/),
and modulus (%).
2. Relational Operators: Relational operators compare two values and return a boolean result
indicating the relationship between them. Examples include greater than (>), less than (<), equal
to (==), not equal to (!=), etc.
3. Logical Operators: Logical operators perform logical operations on boolean values. Examples
include AND (&&), OR (||), and NOT (!).
4. Bitwise Operators: Bitwise operators manipulate individual bits of integer operands. Examples
include bitwise AND (&), bitwise OR (|), bitwise XOR (^), and bitwise complement (~).
5. Assignment Operators: Assignment operators are used to assign values to variables. Examples
include simple assignment (=), addition assignment (+=), subtraction assignment (-=), etc.

Understanding and mastering these data types and operators are essential for writing effective and
efficient Java programs. They form the foundation upon which more complex programming concepts are
built. By the end of this lab, you will have gained a thorough understanding of Java data types and
operators, laying a strong foundation for your journey into more advanced Java programming concepts.
Remember to actively engage with each task, experiment with different scenarios, and seek clarification
whenever needed.

Tasks:

Task #1:

Writing a program that stores a student’s year (Freshman, Sophomore, Junior, or Senior), the
number of courses the student is taking, and his or her GPA on a 4.0 scale. Declare variables
with the appropriate names and types to hold this information. Print the stored information at the
end.

Task #2:

Write an application that inputs one number consisting of five digits from the user, separates the
number into its individual digits and prints the digits separated from one another by three spaces
each. For example, if the user types in the number 42339, the program should print:

4 2 3 3 9

Assume that the user enters the correct number of digits. What happens when you execute the
program and type a number with more than five digits? What happens when you execute the
program and type a number with fewer than five digits?

Task #3:

CS 212: Object Oriented Programming Page 3


In physics, a common useful equation for finding the position s of a body in linear motion at a
given time t, based on its initial position s0, initial velocity v0, and rate of acceleration a, is the
following:
1 2
s=s 0 +v 0 t+ a t
2

Write code to declare variables for s0 , v 0, a, and t, and then write the code to compute s on the
basis of these values.

Task #4:

(Tabular Output) Write a Java application that uses looping to print the following table of
values:

Task #5:

(Palindromes) A palindrome is a sequence of characters that reads the same backward as


forward. For example, each of the following five-digit integers is a palindrome: 12321, 55555,
45554 and 11611. Write an application that reads in a five-digit integer and determines whether
it’s a palindrome. If the number is not five digits long, display an error message and allow the
user to enter a new value.

Task #6:

(Printing the Decimal Equivalent of a Binary Number) Write an application that inputs an
integer containing only 0s and 1s (i.e., a binary integer) and prints its decimal equivalent. [Hint:
Use the remainder and division operators to pick off the binary number’s digits one at a time,
from right to left. In the decimal number system, the rightmost digit has a positional value of 1
and the next digit to the left a positional value of 10, then 100, then 1000, and so on. The decimal
number 234 can be interpreted as 4 * 1 + 3 * 10 + 2 * 100. In the binary number system, the
rightmost digit has a positional value of 1, the next digit to the left a positional value of 2, then 4,
then 8, and so on.
The decimal equivalent of binary 1101 is 1 * 1 + 0 * 2 + 1 * 4 + 1 * 8, or 1 + 0 + 4 + 8 or, 13.]

CS 212: Object Oriented Programming Page 4


Task #7:

(Factorial) Write an application that reads a nonnegative integer and computes and prints its
factorial.

CS 212: Object Oriented Programming Page 5

You might also like