Sunteți pe pagina 1din 10

Basecamp Capability 1 Assignments

Introduction:
This document covers list of assignments/exercises related to first capability of
Basecamp program:

 Use programming elements to store, load and clear data

Observations:
KO: 1. Ability to implement right program structure - initialize variables, type casting,
and data types
KO: 2. Ability to work with different data types - String and primitive data types
KO: 3. Ability to work with Loop - while, do while and for loops
KO: 4. Ability to work with conditional statements - if else, else if ladder and switch case

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

Exercise 1: Factorial of a number


Write a program to calculate factorial of a number.
Example: 5! = 5*4*3*2*1.
Input:
Initialize/receive an integer value
Output:
Factorial of a given number is 120.

Exercise 2: Multiplication Table


Write a program that accepts an integer as input and prints the table of that integer up
to 12. For example, is the user enters 7, the output should be:

pg. 1
Basecamp Capability 1 Assignments

7x1=7
7 x2 = 14
.....
7 x 12 = 84

Exercise 3: Hailstone Sequence


Write a program that reads in a number from the user and then displays the Hailstone
sequence for that number. The problem can be expressed as follows:
 Pick some positive integer and call it n.
 If n is even, divide by two.
 If n is odd, multiply by three and add one.
 Continue this process until n is equal to one.
Your program should be able to produce a sample run that looks like this:

pg. 2
Basecamp Capability 1 Assignments

Input:
Input is a positive number
Output:
Output is a series of steps showing how it reached the number and then should return
total count of steps

Exercise 4: How to get input from user in Java?


Analyze following two programs to understand how to get different types of input from
keyboard in Java.

Program 1: KeyboardUtil.java

import java.util.Scanner;

public class KeyboardUtil {

/**
*
* @param message
* message to print on console
* @return int fetched from keyboard
*/
public static int getInt(String message) {
System.out.println(message);
Scanner scanner = new Scanner(System.in);
return scanner.nextInt();
}

/**
*
* @param message
* message to print on console
* @return Double fetched from keyboard
*/
public static double getDouble(String message) {
System.out.println(message);
Scanner scanner = new Scanner(System.in);
return scanner.nextDouble();
}

pg. 3
Basecamp Capability 1 Assignments

/**
*
* @param message
* message to print on console
* @return String fetched from keyboard
*/
public static String getString(String message) {
System.out.println(message);
Scanner scanner = new Scanner(System.in);
return scanner.nextLine();
}

Program 2: KeyboardDemo.java

public class KeyboardDemo {

public static void main(String[] args) {

int number1 = KeyboardUtil.getInt("Enter a number");

int number2 = KeyboardUtil.getInt("Enter a number");

int sum = number1 + number2;


System.out.println(sum);
}

Exercise 5: Introducing Functions


Write a program to accept employee id, job band and department code and to validate
the job band and department code using functions/methods. The valid job bands are
“C1”,”C2”,”C3” and “C4”. The department code must be in the range 110 – 125. If all
input are valid then display employee details.
Input:
Employee ID, Job band and department code

pg. 4
Basecamp Capability 1 Assignments

Output:
Error message if any of input is invalid otherwise Employee details

Exercise 6: LCM and GCD


Write a program which accepts two integer numbers and displays their LCM and GCD.
Create methods/functions.
Input:
4, 6
Output:
LCM: 12
GCD: 2

Exercise 7: Fibonacci number


Write a method that returns the sum of all even Fibonacci numbers. Consider all
Fibonacci numbers that are less than or equal to n.
Each new element in the Fibonacci sequence is generated by adding the previous two
elements.
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, etc.

public static int evenFibonacciSum(Integer n){}

Exercise 8: Convert algorithm to Program


Convert following algorithm into executable program:
Step 1: Get input ‘a’ and ‘b’
Step 2: print “invalid’ and exit if a> b
Step 3: print “invalid” and exit if a<=0 or b<=0
Step 4: do following task for 10 times

pg. 5
Basecamp Capability 1 Assignments

a=a+b;
b=b+10;
Print value of a and b
End

Exercise 9: Lottery Ticket

You have a green lottery ticket, with int a, b, and c on it. Write a method which returns
appropriate integer number based on following conditions:
 If the numbers are all different from each other, the result is 0.
 If all of the numbers are the same, the result is 20.
 If two of the numbers are the same, the result is 10.

It should support following test cases:

pg. 6
Basecamp Capability 1 Assignments

Exercise 10: Pyramid structure

Write a program to make a pattern like a pyramid with a number which will repeat the
number in the same row
1
2 2
3 3 3
4 4 4 4

Exercise 11: Number of digits

Write a program that reads a positive integer and count the number of digits the
number.

Exercise 12: Find the output

pg. 7
Basecamp Capability 1 Assignments

Exercise 13: Find the output

Exercise 14: Find the output

pg. 8
Basecamp Capability 1 Assignments

Exercise 15: Find the output

Exercise 16: Find the output

pg. 9
Basecamp Capability 1 Assignments

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

Summary:
You must have learnt following concepts:
 How to write a basic program?
 How to declare primitive variables?
 How to get input from console and display?
 How to use conditional and iterative statements?
 How to define functions/methods?
 How to pass arguments to functions?
 How to work with local variables?
 How to do typecasting?

pg. 10

S-ar putea să vă placă și