0% found this document useful (0 votes)
26 views8 pages

MCA 109 Lab Assignment Batch 24-26

Bbb jjj njnj nnnn nnn nnnn

Uploaded by

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

MCA 109 Lab Assignment Batch 24-26

Bbb jjj njnj nnnn nnn nnnn

Uploaded by

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

MCA-167 Lab Assignments

Assignment Set: 1

Objectives of the Assignment:


• Implementing the basic Object Oriented Programming concepts in Java using
Notepad++ and JDK toolkit.
• Developing programs to evaluate the concept implementation of Wrapper
Classes, Type Casting and Java Libraries.

CO/BTLCovered: CO1/BTL3 & BTL5

Beginner’s Problems

BP1 Write a single line of code that prints the result of x minus y. x and y are int
variables that have already been declared and initialized.

BP2 Write a snippet of code that:


declares a variable mark of type char and initializes it to A
declares a variable named grade of type double and initializes it to 98.8
declares a variable age of type int and initializes it to 42
declares a variable named summerIsOver of type boolean and initializes it to
true
Display these variable values on the console

BP3 Create a class where two int variables have been declared and initialized:
increaseMe and decreaseMe. The value of increaseMe should be increased by
the original value of decreaseMe. The value of decreaseMe should be
decreased by the original value of increaseMe, before increasing it. You do not
need to print the original value of the variables.

Hint: you will need to declare at least one more variable.

BP4 Computers can do basic math, quickly and with near-perfect accuracy. This is
one of the many things that makes them so useful.

Learning to program takes time and energy, but as long as you keep trying,
you'll get there. As famous inventor Thomas Edison once said, genius is 1%
inspiration and 99% perspiration. Using that formula, compute the genius
level as a number between 0 and 100 given two double variables: inspiration
and perspiration, each between 0 and 1, and save it a double variable named
genius. For example, given inspiration 1.0 and perspiration 0.5, you would set
genius to 50.5.

BP5 Given an int variables n and m that are at least three digits long, write a
snippet of code that prints "Same" if n and m have the same digit in their
hundreds place. Otherwise, print "Different".

For example if the numbers are 1234 and 4238, you should print "Same".

BP6 Write a simple loop. Assuming an int variable named repeat has been
declared and initialized to a value larger than or equal to zero, write a loop
that prints "Victory!" repeat times on separate lines. You may use any kind of
loop you want! But do not modify the value of repeat.
Practice Problems:
PQ1 Explore the basic java program development scenario in Notepad and cmd by
creating a FinancialCalc. Your calculator should calculate the value of investing
an initial sum of money at a specified interest rate and for a specified number
of years.
a) the final value (V) of an investment (principal P) compounded yearly for
Y years at interest rate I is given by the formula:
V = P (1 + I)Y
b) Make another method and call it in main()
static double computeFinalValue (double principal, double interestRate, int
numOfYears) {
/* YOUR CODE HERE */
}.
Look at the method below which finds prime numbers. It can be declared in the
PQ2 source file Primes.java.
static int findPrimes(int n, boolean printPrimes) {
boolean isPrime = true;
int numPrimes = 0;
for (int i = 2; i <= n; i++) {
isPrime = true;
for (int j = 2; j < i; j++) {
if (i % j == 0) {
isPrime = false;
break; }}
if (isPrime) {
++numPrimes;
if (printPrimes)
System.out.println(i);
}}
return numPrimes;}
Run the code and observe its behavior. What does the variable numPrimes
compute? What does the argument printPrimes mean?
Implement the new method findPrimesFaster by copying code from the
findPrimes method and modifying it to have the following features:

Uses labeled continue instead of break.


Does not require the isPrime variable.
Only tries to divide by integers up to the square root of the number being
tested.
Suppose an int variable n has already been declared and contains a positive
PQ3 integer. If n is divisible by 2, print "Divisible by 2". Print similar messages if it is
divisible by 3 or 4.

PQ4 Create a new Java class called TempConverter. Add a main method to TempConverter
that declares and initializes a variable to store the temperature in Celsius. Your
temperature variable should be store numbers with decimal places.

PQ5 Write a program to enter the numbers till the user wants and at the end it
should display the count of positive, negative and zeros entered.

PQ6 A java standalone application makes use of a parameterized method inside a


class. Take the following case: Create a class Box and define a method in this
class which will return the volume of the box. Initialize two objects for your class
and print out the volumes respectively.
PQ7 Your grade in MCA 109 is based on three components, which are already stored
in double variables: quizScore, homeworkScore, and testScore, each as numbers
between 0 and 1. Quizzes, homework, and the MP are worth 40%, 30%, and 30%
of your grade, respectively. Write a class GradeStudent with method named
calGrade() to calculate your grade as a percentage between 0 and 100.
Advanced Problems
PAQ1 As I Was Going to St. Ives

"As I was going to St Ives" is a traditional English language nursery rhyme


which is generally thought to be a riddle.
The most common modern version is:
As I was going to St. Ives
I met a man with seven wives
Each wife had seven sacks
Each sack had seven cats
Each cat had seven kits
Kits, cats, sacks, wives
How many were going to St. Ives?
The answer to the riddle is commonly thought to be 1, the narrator of the
rhyme. The man, wives, cats and kits are thought to be going the other way
(i.e. coming from St. Ives).

For the final output of the lab, print out the information about who the narrator
"met" while going to St. Ives. That is, print out:

• The number of wives


• The number of sacks
• The number of cats
• The number of kits
• and, the total number of living things met
(i.e. totaling the man, the wives, the cats and the kits)

You are required to use variables, multiplication and addition operations to


determine the answers. For example:
int numMan, numWives; //declaring variables

numMan = 1; // determining the number of men


numWives = numMan * 7; // determining the number of wives
Just storing the value of 7 into the variable numWives is not enough for full
credit for the assignment.

You are also required display the values in the variables when showing the
information about who the narrator met. When doing this you must also
display some text describing any values prior to displaying the values. For
example:
System.out.println ("The number of wives are: " + numWives);

PAQ2 To manipulate data, computers require that it be digitized, or converted into


numbers. But humans have been digitizing the world around us long before
computers came around.

Location is one example. The idea of a geographic coordinate system goes back
to the 3rd century B.C. Geographic coordinate systems represent a horizontal
position on Earth using two numbers: latitude and longitude. (Representing
altitude requires a third number, but let's not worry about that right now.)
Representing location numerically has always enabled exploration—from the
days of the ancient Greeks, to us wandering but never lost with the help of our
smartphone and its navigation apps.

To complete the problem below, you should examine the values of two double
variables that have already been declared and set for you: latitude and
longitude. If their values indicate that you are at a very special place, you should
print "Center of the Universe". Otherwise, print "Somewhere else". Also keep in
mind that latitude and longitude values can be positive and negative when
digitized, and you may need to consider this when formulating your solution.
PAQ3 Two players have completed a game. The score of Player 1 is saved in a int
variable first, and the score of Player 2 in an int variable second. If either player
scored more points than the other, they are the winner! However, if both players
tie, then the player that played second is the winner. You also have access to a
boolean variable firstStarted that is set to true if Player 1 played first and false
otherwise.

Record the result of the game in an existing int variable winner, which you
should set to 1 if Player 1 won and 2 if Player 2 won. (Do not declare winner,
simply set its value appropriately.)
PAQ4 A "hailstone sequence" is defined as follows: Given any starting positive integer,
the next term in the sequence is obtained from the previous by the following
two rules:
If the previous term is even, the next term is equal to the previous term divided
by two.
If the previous term is odd, the next term is equal to three times the previous
term, plus one (3*previous + 1).
These sequences concern one of the most famous unsolved problems in
mathematics: the Collatz conjecture, named after German mathematician Lothar
Collatz who posed the question in 1937. The Collatz conjecture hypothesizes
that, no matter the starting number, all such sequences eventually reach the
number 1.

Given a positive int variable n, write a program that prints out each term in the
hailstone sequence starting from n until 1 is reached (i.e. 1 should always be the
last term printed).
Assignment Set: 2

Objectives of the Assignment:


• Developing programs to evaluate the concept implementation of Arrays, String
arrays, Data Abstraction, Inheritance, Polymorphism, super keyword.

CO/BTLCovered: CO1/BTL3 & BTL5

Beginner’s Problems

BP7 First, declare an array of ints named numbers and initialize it to contain the
values 0, 8, 9, 4, and 5, in that order. Second, given an existing array of
doubles named values, print its first value and also change its third value to
1.25.
Given int variables len, first, and last, create an array nums of ints of length
BP8 len and assign its first element to the value of first and its last element to the
value of last. Assume that the value of len is at least 2.

BP9 You are given a possibly empty array of chars named symbols and an int
variable i. If i represents a valid index in the array, print out the value of the
element at that index. Otherwise, print out "Invalid index"

BP10 Given an char[] named characters that has been declared, initialize the same,
print that array backwards, but with all characters on the same line. As a
reminder, System.out.print will print without advancing to the next line.
Practice Problems:
PQ8 Given an int array named nums, determine if it contains any values between 700
and 799, inclusive. If it does, print "Found".

The phrase "Found" should only be printed once, even if multiple elements in the
array are in the seven-hundreds.

Additionally, after searching the array you should print "Done", regardless if an
element in the seven-hundreds was found or not.
Create a method named catchupGrading that accepts an array of double values
PQ9 and implements the following catch-up grading policy. Given a score on Quiz
N, if a student does better on the next quiz (Quiz N + 1), then you should
replace their score on Quiz N with the average of their score on Quiz N and
their score on Quiz N + 1. Otherwise, their score on Quiz N is unchanged.
Return the number of times that the student does strictly better on the next quiz
than they did on the previous one.

Scores are stored in the array in order. So, given an array with the scores {100.0,
80.0, 90.0}, you would modify the array to contain {100.0, 85.0, 90.0}, and return
1.
Complete a method named arrayFill3Pattern that creates int arrays filled with a
PQ10 3-based pattern. Here are some examples:
1

147
4 7 10

14
47
7 10
10 13
And so on.
Your method should accept two int parameters: the number of rows and
columns in the array. Both values will be positive. Return a two-dimensional int
array with the first index the rows and the second the columns filled with the
pattern as shown above.
PQ11 Create a public class called Student that inherits from a class called Person. (Do
not create Person. It is already available.) Define a single public Student
constructor that takes a String argument (name) and an int argument (university
ID number). You should call the Person constructor and pass that String
argument. (You don't need to do anything else with it.) You should also provide
a public getter named getID for the student ID. Reject negative ID numbers
PQ12 Create a public class named Orderer that provides a single class method named
order. order should accept a single parameter, a Restaurant to order from, and
return a String, a comment on your order. Depending on which subclass of
Restaurant it is, you should respond differently:

If the restaurant is a Fancy restaurant—for instance, with name "MIGA"—you


should order "At MIGA I'll order something inexpensive"
If the restaurant is a FastFood restaurant, for instance, with with name
"Chipotle"—you should order "At Chipotle I'll order something healthy"
If the restaurant is a Vegan restaurant, then it will have a String property cuisine
you can retrieve using getCuisine. For example, if it its cuisine is "Thai" and
name is "Vegan Delight", you should order "At Vegan Delight I'll order delicious
Thai food".
All Restaurants have a name that you can retrieve using getName. If the
restaurant is null or not one of the kinds described above, return null. Do not
solve this problem using method overloading. And do not hard-code the
answers. Your solution should work for any Fancy, FastFood, or Vegan instance.
PQ13 Define a public class named Flip with a single public instance method named
flop that takes no parameters and returns a boolean. Flip should also provide a
single public constructor that accepts a boolean argument and sets the initial
state of the Flip instance.

Flip maintains one piece of private state: the boolean. Calling flop changes the
boolean from true to false or false to true and returns the new (not the old) state
of the boolean.
Advanced Problems
PAQ5 Let's compute a few summary statistics on a dataset. Create a method named
summarizeValues that accepts an array of double values. The array will not be
null and will not be empty.

Compute the mean and the range of the dataset and return a String in the
following format:

Mean: <mean>
Range: <range>
Where <mean> and <range> are the mean and range of the dataset computed
by your method.

Round the values to 2 decimal points using a method round which accepts a
double value. For example, round(3.33542) would return 3.34.

Given the array {1.0, 2.0, 4.0}, your method should return:

Mean: 2.33
Range: 3.0
PAQ6 Write a method called smallWordFilter that, given a non-null String containing
words separated by single spaces (" "), returns all the words in the original
String that are 3 characters or shorter in the same order in which they appeared
in the original String, as a String[].

For example, given the input "Xyz is the very best cat" you would return the
String[] {"Xyz", "is", "the", "cat"}. We have skipped both "very" and "best"
because they are longer than 3 characters.

You might also like