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

GIU_2478_61_14171_2023-12-02T08_18_05

Uploaded by

a6900710
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)
13 views8 pages

GIU_2478_61_14171_2023-12-02T08_18_05

Uploaded by

a6900710
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

German International University of Applied Science

Faculty of Informatics and Computer Science


Prof. Dr. Slim Abdennadher

Introduction to Computer Science, Winter 2023


Practice Assignment 5

Exercise 5-1 Square N


To be discussed in Tutorials
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:

* * * * * *
* *
* *
* *
* *
* * * * * *

Solution:
import j a v a . u t i l . S c a n n e r ;
public c l a s s Square
{
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s )
{
S c a n n e r s c = new S c a n n e r ( System . i n ) ;
System . o u t . p r i n t ( " E n t e r s i d e : " ) ;
int n = sc . n e x t I n t ( ) ;
int i , j ;
f o r ( i = 1 ; i <= n ; i ++)
{
f o r ( j = 1 ; j <= n ; j ++)
{
i f ( i == 1 | | i == n | | j == 1 | | j == n )
System . o u t . p r i n t ( " * " ) ;
else
System . o u t . p r i n t ( " " ) ;
}
System . o u t . p r i n t l n ( ) ;
}
}
}

Exercise 5-2 Pyramid


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

1
12345
1234567
123456789

Solution:
import j a v a . u t i l . * ;
public c l a s s TriangleNumbers {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s )
{
String line ;
S c a n n e r s c = new S c a n n e r ( System . i n ) ;
System . o u t . p r i n t l n ( " P l e a s e e n t e r a number b e t w e e n 1 and 9 " ) ;
int n = sc . n e x t I n t ( ) ;

int i , j , z ;
f o r ( i = 1 ; i <= ( n / 2 ) + 1 ; i ++)
{
f o r ( z = ( n /2) − i + 1 ; z > 0 ; z−−)
System . o u t . p r i n t ( " " ) ;
f o r ( j = 1 ; j <= ( i * 2 −1); j ++)
System . o u t . p r i n t ( j ) ;
System . o u t . p r i n t l n ( ) ;
}

}
}

Another s o l u t i o n :

import j a v a . u t i l . * ;
public c l a s s TriangleNumbers {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s )
{
String line ;
S c a n n e r s c = new S c a n n e r ( System . i n ) ;
System . o u t . p r i n t l n ( " P l e a s e e n t e r a number b e t w e e n 1 and 9 " ) ;
int n = sc . n e x t I n t ( ) ;

int i , j , z ;
i n t odd = 1 ;
f o r ( i = 1 ; i <= ( n / 2 ) + 1 ; i ++)
{
f o r ( j = i ; j <=n / 2 ; j ++)
System . o u t . p r i n t ( " " ) ;
f o r ( z = 1 ; z <= odd ; z ++)
System . o u t . p r i n t ( z ) ;
System . o u t . p r i n t l n ( ) ;
odd +=2;
}

}
}

2
Exercise 5-3 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

Solution:
import j a v a . u t i l . * ;
p u b l i c c l a s s WordFinder {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s )
{
S c a n n e r s c = new S c a n n e r ( System . i n ) ;
System . o u t . p r i n t l n ( " P l e a s e e n t e r t h e s e n t e n c e " ) ;
S t r i n g l i n e = sc . nextLine ( ) ;
System . o u t . p r i n t l n ( " P l e a s e e n t e r t h e key word " ) ;
S t r i n g word = s c . n e x t L i n e ( ) ;

int lineLength = line . length ( ) ;


i n t w o r d L e n g t h = word . l e n g t h ( ) ;
i n t wordCounter = 0;

/ / Consider the ’. ’ value v a r i a t i o n . In case the user f o r g e t s i t .


i f ( l i n e . c h a r A t ( l i n e L e n g t h −1) ! = ’ . ’ )
{
line = line+’ . ’ ;
l i n e L e n g t h ++;
}
int i = 0;
int pointer = 0;
char c ;
S t r i n g toBeCompared ;

w h i l e ( i < l i n e L e n g t h −1)
{
/ / Get word by word
do
{
c = l i n e . charAt ( p o i n t e r ) ;
p o i n t e r ++;
} w h i l e ( ( c ! = ’ ’ ) && ( c ! = ’ ; ’ ) && ( c ! = ’ . ’ )
&& ( c ! = ’ , ’ ) && ( p o i n t e r < l i n e L e n g t h ) ) ;

/ / s u b s t r i n g ( x , y ) r e t u r n s t h e word s t a r t i n g f r o m x t o y−1
toBeCompared = l i n e . s u b s t r i n g ( i , p o i n t e r − 1 ) ;

/ / Now , compare o n l y i f t h e word l e n g t h e q u a l s t h e key−word l e n g t h


i f ( w o r d L e n g t h == toBeCompared . l e n g t h ( ) )
{
/ / Comparing t h e words i g n o r i n g Upper and Lower c a s e s
i f ( word . e q u a l s I g n o r e C a s e ( toBeCompared ) == t r u e )
w o r d C o u n t e r ++;

3
}
i = pointer ;
}
System . o u t . p r i n t l n ( " Number o f O c c u r e n c e : " + w o r d C o u n t e r ) ;
}
}

Exercise 5-4 Number of Digits


To be solved in the labs
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!

Solution:
import j a v a . u t i l . * ;
public class DigitCounter {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s )
{
System . o u t . p r i n t l n ( " P l e a s e e n t e r t h e f i r s t number " ) ;
i n t num1 = s c . n e x t I n t ( ) ;
i n t c o u n t , num2 ;

w h i l e ( num1 ! = −1)
{
count = 1;
num2 = num1 ;

w h i l e ( num2 / 1 0 ! = 0 )
{
num2 = num2 / 1 0 ;
c o u n t ++;
}
System . o u t . p r i n t l n ( " Number o f d i g i t s i n " + num1 + " i s " + c o u n t ) ;
System . o u t . p r i n t l n ( " P l e a s e e n t e r t h e n e x t number " ) ;
num1 = s c . n e x t I n t ( ) ;
}
}
}

4
Exercise 5-5 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.

Solution:
public class MostDivisors {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s )
{
int maxDivisors = 1;
i n t numWithMax = 1 ;
f o r ( i n t n = 2 ; n <= 1 0 0 0 0 ; n ++)
{
int divisorCount = 0;
f o r ( i n t d = 1 ; d <= n ; d ++)
{
i f ( n % d == 0 )
d i v i s o r C o u n t ++;
}

i f ( divisorCount > maxDivisors )


{
maxDivisors = divisorCount ;
numWithMax = n ;
}
}
System . o u t . p r i n t l n ( "Among i n t e g e r s b e t w e e n 1 and 1 0 0 0 0 , " ) ;
System . o u t . p r i n t l n ( " The max number o f d i v i s o r s i s " + m a x D i v i s o r s ) ;
System . o u t . p r i n t l n ( "A num w i t h " + m a x D i v i s o r s + " d i v i s " + numWithMax ) ;
}
}

Exercise 5-6 Extract Numbers


To be solved in the labs
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
The numbers contained in your string are
365
12

Solution:
import j a v a . u t i l . * ;
public c l a s s ExtractNumbers {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s )
{
S c a n n e r s c = new S c a n n e r ( System . i n ) ;
System . o u t . p r i n t l n ( " P l e a s e e n t e r y o u r s t r i n g " ) ;

5
S t r i n g s = sc . nextLine ( ) ;

int flag = 0;

f o r ( i n t i = 0 ; i < s . l e n g t h ( ) ; i ++ )
{
/ / t h e end o f t h e s t r i n g i s n o t r e a c h e d and t h e r e i s a number b e t w e e n 0 and
w h i l e ( i < s . l e n g t h ( ) && s . c h a r A t ( i ) <= ’ 9 ’ && s . c h a r A t ( i ) >= ’ 0 ’ )
{
System . o u t . p r i n t ( s . c h a r A t ( i ) ) ;
i ++;
f l a g ++;
}
i f ( f l a g > 0)
{
flag = 0;
System . o u t . p r i n t l n ( ) ;
}
}
}
}

Exercise 5-7 Run Length


To be discussed in the tutorials

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

Solution:
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s )
{
String input = "
WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW
";

i n t i =0;
i n t count =0;
char c u r r e n t ;
S t r i n g o u t p u t =" " ;
while ( i < i n p u t . l e n g t h ( ) )
{
c u r r e n t = input . charAt ( i ) ;
w h i l e ( i < i n p u t . l e n g t h ( ) && i n p u t . c h a r A t ( i ) == c u r r e n t )
{
c o u n t ++;
i ++;
}

o u t p u t += c o u n t + " " + c u r r e n t ; / / update output


count =0; / / r e s e t counter
}

6
System . o u t . p r i n t l n ( " I n p u t : " + i n p u t ) ;
System . o u t . p r i n t l n ( " O u t p u t : " + o u t p u t ) ;

b) Moreover, write a Java program that reverses the compression.


Example:

Input: 12W1B12W3B24W1B14W
Output: WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW

Solution:
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s )
{
S t r i n g i n p u t = " 12W1B12W3B24W1B14W" ;

i n t i =0;
i n t count =0;
char c u r r e n t ;
S t r i n g o u t p u t =" " ;
S t r i n g temp= " " ;
while ( i < i n p u t . l e n g t h ( ) )
{
c u r r e n t = input . charAt ( i ) ;

/ / f i n d count n
w h i l e ( i < i n p u t . l e n g t h ( ) && i n p u t . c h a r A t ( i ) <=57 && i n p u t
. c h a r A t ( i ) >=48) / / i t ’ s a d i g i t
{
temp += i n p u t . c h a r A t ( i ) ;
i ++;
}
c o u n t = I n t e g e r . p a r s e I n t ( temp ) ;

/ / get character
c u r r e n t = input . charAt ( i ) ;

/ / concatenate n times
f o r ( i n t j = 0 ; j < c o u n t ; j ++)
o u t p u t += c u r r e n t ;

/ / r e s e t and u p d a t e
temp= " " ;
count =0;
i ++;

}
System . o u t . p r i n t l n ( " I n p u t : " + i n p u t ) ;
System . o u t . p r i n t l n ( " O u t p u t : " + o u t p u t ) ;

Exercise 5-8 SubString


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

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

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

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

Solution:
import j a v a . u t i l . * ;
public class subString {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s )
{
S c a n n e r s c = new S c a n n e r ( System . i n ) ;
S t r i n g s1 = sc . n e x t L i n e ( ) ;
S t r i n g s2 = sc . n e x t L i n e ( ) ;
boolean f = f a l s e ;
f o r ( i n t i = 0 ; i < s 2 . l e n g t h ( ) ; i ++)
{
S t r i n g temp = " " ;
f o r ( i n t j = 0 ; j < s 1 . l e n g t h ( ) && ( i + s 1 . l e n g t h ( ) ) <= s 2 . l e n g t h ( ) ; j ++)
{
temp += s 2 . c h a r A t ( i + j ) ;
}
i f ( temp . e q u a l s ( s 1 ) )
f = true ;
}
System . o u t . p r i n t l n ( f ) ;
}
}

You might also like