Uploaded by januarysunsett

JAVA Questions and answers

advertisement
Java typed questions and answers
11 qns
QUESTION ONE
Write brief notes on the following types of Agile Methodologies
i.
Extreme Programming (Beck)
ii.
SCRUM (Schwaber and Beedle)
iii.
Lean Software Development
iv.
Dynamic Systems Development Methodology
v.
Crystal Methodologies (Cockburn)
vi.
Adaptive Software Development (Highsmith)
vii.
Feature-Driven Development (Palmer and Felsing)
1(i)
Extreme programming is a software development methodology which is intended to improve
software quality and responsiveness to changing customer requirements. As a type of
agile software development, it advocates frequent "releases"
in short Kent Beck developed extreme programming during his work on the Chrysler. Extreme
Programming is an agile software development framework that aims to produce higher
quality software, and higher quality of life for the development team. It is the most specific of
the agile frameworks regarding appropriate engineering practices for software development.
1(ii)
Scrum is a management and control process that cuts through complexity to focus on building
software that meets business needs.
The main idea is to enable development teams to focus on development without distractions or
interruptions from management or anywhere else.
1(iii).
Lean Software Development is an agile framework, intended to optimize development time, use
of resources, and focus on the minimum viable needs of a
Lean software development is a translation of lean manufacturing principles and practices to
the software development domain. Adapted from the Toyota Production System.
The Lean Development Methodology: Decrease Costs, Effort, and Waste. Lean software
development is a set of principles that can be applied to software development to decrease
programming effort, budgeting, and defect rates by one third.
It is based on the principle of eliminating waste, build quality, create knowledge, defer
commitment, deliver fast, respect people and optimize the whole value stream.
1(iv)
Dynamic Systems Development Methodology is an agile method that focuses on the full project
lifecycle and its impact on the business. It is an agile method that addresses the needs of both
simple product developments where teams are likely to be co-located, and more complex project.
1(v)
Crystal Methodologies (Cockburn)
Crystal method is an agile software development approach that focuses primarily on people and
their interactions when working on a project rather than on processes and tools. Alistair believed
that people's skills and talents as well as the way they communicate has the biggest impact on the
outcome of the project. Crystal methods are considered and described as lightweight
methodologies Crystal methods are focused on: People, Interaction, Community, Skills, Talents
Communications
1(vi)
Adaptive Software Development (Highsmith)
Is a software development that grew out of the work by Highsmith and embodies the principle
that continuous adaptation of the process to the work at hand is the normal state of affairs. It
provides for continuous learning and adaptation to the emergent state of the project.
1(vii)
Feature-Driven development (palmer and Felsing).
An agile method that combine the key advantages of agile methodologies with techniques that
scale to the largest teams and projects. It is a development methodology based on object model,
feature list, dynamic feature teams, and milestones
QUESTION TWO
Write a program called Swap2Num that prompts user for two numbers. The program shall read
the inputs as int, save in two variables called num1 and num2; swap the contents of the two
variables; and print the results.
Import java. util. Scanner;
package swapnum2;
public class Swapnum2 {
public static void main (String [] args) {
int num1, num2;
Scanner in = new Scanner(System.in);
System.out.println("Input the first number;");
num1 = in. nextInt ();
System.out.println("Input the second number;");
num2 = in. nextInt ();
System.out.println("swapped numbers are 2;"+num1);
System.out.println("and;" +num2);
}
}
QUESTION THREE
The progressive income tax rate is set as follows
Taxable Income
Rate (%)
First shs.200,000
0
Next shs.200,000
10
Next shs.200,000
20
The remaining
30
For example, suppose that the taxable income is 850000, the income tax payable is 200000*0%
+ 200000*10% + 200000*20% + 250000*30%.
Write a program called IncTaxCalculator that reads the taxable income (in int). The program
shall calculate the income tax payable (in double); and print the result.
import.java. util. Scanner;
public class Income {
public static void main (String [] args) {
double tax=0, it;
Scanner sc=new Scanner(System.in);
System.out.println("Enter income ");
it=sc. nextDouble ();
if(it<=200000)
tax=0;
else if(it<=400000)
tax=0.1*(20000) + (0.0*200000);
else if(it<=600000)
tax= (0.2*(200000)) +(0.1*200000) + (0.0*200000);
else if(it<=850000)
tax= (0.3*(250000) +(0.2*200000) +(0.1*200000) +(0.0*200000);
System.out.println("Income tax amount is "+tax);
}
}
Output
Enter income
850000
Income tax amount is 135000.0
QUESTION FOUR
Both the employer and the employee contribute a certain percentage of the employee's salary
towards NSSF. The rate is tabulated as follows:
Employee’s Age
Employee Rate(%)
Employer Rate(%)
20
17
Above 55 to 60
13
13
Above 60 to 65
7.5
9
Above 65
5
7.5
55 and below
Write a program called NssfContributionCalculator that reads the monthly salary and age
(in int) of an employee. Your program shall calculate the employee's, employer's and total
contributions (in double); and print the results.
package Nssfcontributioncalculator;
import java. util. Scanner;
public class NssfContributionCalculator {
public static void main (String [] args) {
{
{
double ag, ee, er, total contribution;
Scanner sc=new Scanner(System.in);
System.out.println("enter Age");
ag=sc. nextDouble ();
System.out.println("enter Employee Rate %");
ee=sc. nextDouble ();
System.out.println("enter employers rate %");
er=sc. nextDouble ();
if (ag <= 55) {
ee=ee*(0.2);
er=er*(0.17);
}
else if (ag <= 60) {
ee=ee*(0.13);
er=er*(0.13);
}
else if (ag <= 65) {
ee=ee*(0.075);
er=er*(0.09);
}
else if (ag>= 65) {
ee= ee*(0.05);
er= er*(0.075);
}
total contribution=ee+er;
System.out.println("total contribution is="+total contribution);
}
}
}
QUESTION FIVE
Write a program that prompts user for a positive integer. The program shall read the input as int;
and print the "reverse" of the input integer.
package integers;
import java. util. Scanner;
public class Integers {
}
public static void main (String [] args) {
{
int n;
Scanner s = new Scanner(System.in);
System.out.print("Enter the number you want to check:");
n = s. nextInt ();
if (n > 0)
{
System.out.println("The given number "+n+" is Positive");
}
else if (n < 0)
{
System.out.println("The given number "+n+" is Negative");
}
else
{
System.out.println("The given number "+n+" is neither Positive nor Negative ");
}
}
}
QUESTION SIX
Write a program that prompts user for a positive integer. The program shall read the input as int;
compute and print the sum of all its digits.
package sumpositivenumbers;
import java. util. Scanner;
public class SumPositiveNumbers {
public static void main (String [] args) {
{
{
int num, sum=0;
Scanner input = new Scanner(System.in);
System.out.print("Enter Numbers (Negative Number to Quit):");
while(true)
{
num=input. nextInt (); //Read number
if(num<0)
break;
sum += num;
}
System.out.println(“Sum is: "+sum);
}
}
QUESTION SEVEN
Write a program called TimeTable that prompts user for the size (a positive integer in int); and
prints the multiplication table as shown:
Enter the size: 5
*
1
2
3
4
5
1
1
2
3
4
5
2
2
4
6
8
10
3
3
6
9
12
15
4
4
8
12
16
20
5
5
10
15
20
25
public class TimeTable {
public static void main (String [] args) {
TimeTable aTimeTable = new TimeTable ();
aTimeTable.printTimeTable(5);
}
private void printTimeTable (int n)
{
for(int i = -1; i <= n; i++)
{
if (i == -1) {
System.out.print("\t|");
}
else if (i == 0) {
System.out.print("------------");
}
else if (i > 0) {
System.out.printf("%1$d\t|", i);
}
for(int j = 1; j <= n; j++) {
if (i == -1) {
System.out.printf("\t%1$d", j);
continue;
}
else if (i == 0) {
System.out.print("--------");
continue;
}
System.out.printf("\t%1$d", i * j);
}
System.out.println();
}
}
Output;
*
1
2
3
4
5
1
1
2
3
4
5
2
2
4
6
8
1
0
3
3
6
9
1
2 1
5
4
4
8
1
2
1
6 2
0
5
5
1
1
5
2
0 2
5
QUESTION EIGHT
0
Write a program called PrintArray which prompts user for the number of items in an array (a
positive number), and saves it in an int variable called numItems. It then prompts user for
the values of all the items and saves them in an int array called items. The program shall then
print the contents of the array in the form of [x1, x2, ..., xn].
import java. util. Arrays;
class PrintArrays {
public class Array{
public statc void main(String[] args) {
}
int numItems;
int[] items;
numItems = 1 2 3 5 .....
items = new int[numItems];
if (items. Length > 0) {
for (int i = 0; i < items. Length; ++i) {
}
}
for (int i = 0; i < items. length; ++i) {
if (i == 0) {
} else {
System.out.print(i == 0);
}
QUESTION NINE
Write a method called exponent(int base, int exp) that returns an int value of base raises to
the power of exp. The signature of the method is:
public static int exponent(int base, int exp);
Assume that exp is a positive number and base is an integer. Do not use any Math
library functions.
Also write the main() method that prompts user for the base and exp; and prints the
result.
public class Exponent {
public static void main (String [] args) {
int exp;
int base;
System.out.println(base + "raises to the power of " + exp + "is: " + exponent (base, exp));
}
public static int exponent (int base, int exp) {
int product = 1;
{product *= base;
}
return product;
}
}
QUESTION TEN
Write a boolean method called isOdd() in a class called OddEvenTest, which takes an int as
input and returns true if the it is odd. The signature of the method is as follows:
public static boolean isOdd(int number);
package oddeventest;
import java. util. Scanner;
public class OddEvenTest {
public static void main (String [] args) {
int number;
System.out.println("Enter an integer to check if it is odd or even: ");
Scanner input = new Scanner(System.in);
number = input.nextInt();
isOdd(number);
Boolean answer=isOdd(number);
if (answer=true)
{
System.out.println("EVEN");
}
if (answer=false)
{
System.out.println("ODD");
}}
public static Boolean isOdd(int number)
{
if(number % 2 == 0)
{
return true;
}
return false;
}
QUESTION ELEVEN
A positive integer is a prime if it is divisible by 1 and itself only. Write a boolean method
called isPrime(int aPosInt) that takes a positive integer and returns true if the number is a prime.
Write a program called PrimeNum that prompts the user for an upper bound (a positive integer),
and lists all the primes less than or equal to it.
import java. util. Scanner;
public class PrimeNum {
public static void main (String [] args) {
System.out.print("Enter the upper bound (positive integer): ");
Scanner in = new Scanner(System.in);
if ( ! in.hasNextInt()) {
System.out.println("Upper bound not valid. Please try again.");
return;
}
int upperBound = in. nextInt ();
PrimeNum aPrimeList = new PrimeNum ();
PrimeList.printPrimes(upperBound);
}
private Boolean isPrime(int posInt)
{
for (int i = 2; i <= Math.sqrt(posInt); i++)
{
if (posInt%i != 0) {
continue;
}
return false;
}
return true;
}
private void printPrimes (int upperBound)
{
int countPrimes = 0;
for (int i = 1; i <= upper Bound; i++) {
if (i isPrime(i)) {
continue;
}
System.out.println(i);
countPrimes++;
}
System.out.printf("\n[%1$d primes found (%2$.2f%%)]\n"
, countPrimes, ((double)100* countPrimes/upperBound)
);
}
Download
Study collections