Lab 2 BSCS 13C OOP QUESTIONS
Lab 2 BSCS 13C OOP QUESTIONS
Objective: The primary objective of this lab is to provide you with a solid understanding of the following
key concepts:
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: 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.
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:
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:
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.]
(Factorial) Write an application that reads a nonnegative integer and computes and prints its
factorial.