0% found this document useful (0 votes)
24 views4 pages

PA 4

This document outlines a practice assignment for an Introduction to Computer Programming course at the German University in Cairo. It includes various exercises that require students to write Java programs for tasks such as finding maximum and minimum integers, constructing pyramids, checking substrings, and counting occurrences of words. The assignment is scheduled for discussion between March 18 and March 23, 2023.

Uploaded by

maryam.elnahas04
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)
24 views4 pages

PA 4

This document outlines a practice assignment for an Introduction to Computer Programming course at the German University in Cairo. It includes various exercises that require students to write Java programs for tasks such as finding maximum and minimum integers, constructing pyramids, checking substrings, and counting occurrences of words. The assignment is scheduled for discussion between March 18 and March 23, 2023.

Uploaded by

maryam.elnahas04
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/ 4

German University in Cairo

Media Engineering and Technology


Prof. Dr. Slim Abdennadher
Dr. Milad Ghantous
Dr. Mohamed Hamed

Introduction to Computer Programming, Spring Term 2023


Practice Assignment 4
Discussion: 18.03.2023 - 23.03.2023

Exercise 4-1 Stream of Numbers


To be discussed in Tutorials
Write a Java program to read a list of nonnegative integers and outputs the maximum integer, the minimum integer,
and the average of all the integers. The end of the input is indicated by the user entering a negative number.
Note that the negative number is not used in finding the maximum, minimum, or average. The output should be
something like this:

Please enter a sequence of positive numbers


2
3
5
4
-1
The maximum number is : 5
The minimum number is: 2
The average is: 3.5

Use in one program a while loop and in another program a do while loop.

Exercise 4-2 Pyramid


To be discussed in Tutorials
Construct the following pyramid of numbers given that n is an odd input from the user. For example if n=9, the
pyramid should look like the following:

1
123
12345
1234567
123456789

Exercise 4-3 Contains


To be discussed in Tutorials
Write a program that takes two strings s1 and s2 as inputs and checks whether string s1 is a substring in string s2.
Example:

s1 = "abc"
s2 = "ababc"
Output: true

1
s1 = "a"
s2 = "ababc"
Output: true

s1 = "abc"
s2 = "ababa"
Output: false

Exercise 4-4 Run Length I


To be discussed in Tutorials
Write a Java program that reverses the compression of a iven string.
Example:

Input: 12W1B12W3B24W1B14W
Output: WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW

Exercise 4-5 Run Length II


To be solved in Labs
Given a String containing uppercase characters (A-Z), write a Java program that compresses repeated ’runs’ of the
same character by storing the length of that run.

Example:

Input: WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW
Output: 12W1B12W3B24W1B14W

Exercise 4-6 Midterm Spring 2013- Snake Eye


To be solved in the labs
The method Math.random() gives a real number between 0.0 and 0.9999..., and so 6*Math.random() is bet-
ween 0.0 and 5.999.... The type-cast operator, (int), can be used to convert this to an integer: (int)(6*Math.random()).
Thus, (int)(6*Math.random()) is one of the integers 0, 1, 2, 3, 4, and 5. To get a number between 1 and 6, we
can add 1:
(int)(6*Math.random()) + 1
Using the statement above, we would like to know how many times we have to roll a pair of dice before they
come up snake eyes? Note: Snake eyes means that both dice show a value of 1. Write a method that should return
the number of rolls that it makes before the pair of dice come up snake eyes. The method should also display the
following message, e.g.:
It took 100 rolls to get snake eyes.

Note: You have to use a do-while loop.

Exercise 4-7 Square N


To be solved in Labs
Write a Java program to construct a square shape of numbers given that n is an input from the user. For example if
n=6, the shape should look like the following:

2
* * * * * *
* *
* *
* *
* *
* * * * * *

Exercise 4-8 Divisors


To be solved in Labs
Which integer between 1 and 10000 has the largest number of divisors, and how many divisors does it have? Write
a program to find the answers and print out the results. It is possible that several integers in this range have the
same, maximum number of divisors. Your program has to print out one of them.

Exercise 4-9 Word Count


Write a program that reads a sentence and a word from the user and finds the number of occurrences of the given
word in the sentence. For example, the following could be a run of your program

Enter the sentence:


the students are enjoying life at the GIU
Enter the word:
the
The sentence is "the students are enjoying life at the GIU".
The word "the" occurs 2 times in the sentence

Exercise 4-10 Number of Digits


Write a Java program that reads from the user positive integers and count the number of digits in them. The program
should keep asking the user for entering integers until he/she enters -1. The output should be something like this:

Please enter a number


524
Number of digits in 524 = 3
Please enter a number
24
Number of digits in 24 = 2
Please enter a number
35790
Number of digits in 35790 = 5
Please enter a number
-1
Thank you!

Exercise 4-11 Extract Numbers


Write a Java program that takes a string containing text and non-negative numbers form the user and prints out the
numbers contained in the string in separate lines. Use nested loops.
Running example

Please enter your string


The year has 365 days and the day has 12 hours
Output

3
The numbers contained in your string are
365
12

You might also like