You are on page 1of 25

Programme Name: Programming Fundamental

Course Code: CSC- 2604

Course Name: Programming Fundamental

Assignment:5

Deadline of Submission: _______31stAug, 2020______________________

Submitted By: Submitted To:

Student Name: Subin Satyal Faculty Name: BCS (HONS)

IUKL ID: Department: Sunway collage

st
Semester: First (1 )
1. Write a java method that prints hello world. Modify the method so that
the method prints the string that is passed as argument to it.
public class PrintHelloWorld{

public static void main(String [] args){

printHello();

public static void printHello(){

System.out.println("Hello World");

Output:

Hello World

2. Write a Java method that takes two integers as argument and returns
their sum.

import java.util.Scanner;

public class SumMethod{

public static void main(String [] args){

Scanner sc=new Scanner (System.in);

System.out.println("Enter any Two numbers:");

int x=sc.nextInt();

int y=sc.nextInt();

int sum=printSum(x,y);

System.out.println("The sum of the given numbers is:"+sum);

public static int printSum(int x,int y){


int s=x+y;

return s;

Output:

Enter any Two numbers:

25

The sum of the given numbers is:7

3. Write a Java method to find the smallest number among three numbers.
Test Data:
Input the first number: 25
Input the Second number: 37
Input the third number: 29
Expected Output:
The smallest value is 25.0
import java.util.Scanner;
public class SamllestOne{
public static void main(String [] args){
Scanner sc=new Scanner (System.in);
System.out.println("Enter any Three numbers:");
int a=sc.nextInt();
int b=sc.nextInt();
int c=sc.nextInt();
int min=printSmallest(a,b,c);
System.out.printf(" The smallest among three numbers %d, %d, and %d
is : %d %n", a,b,c,min);
}
public static int printSmallest(int a,int b, int c){
int min = a;
if (b < min) {
min = b;
}

if (c < min) {
min = c;
}

return min;
}
}
Output:
Enter any Three numbers:
3 66 2
The smallest among three numbers 3, 66, and 2 is : 2

4. Write a Java method to compute the average of three numbers.


Test Data:
Input the first number: 25
Input the second number: 45
Input the third number: 65
Expected Output
The average value is 45.0
import java.util.Scanner;
public class AverageMethod{
public static void main(String [] args){
Scanner sc=new Scanner (System.in);
System.out.println("Enter any three numbers:");
int x=sc.nextInt();
int y=sc.nextInt();
int z=sc.nextInt();
System.out.printf("Input of first Number:%d",x);
System.out.printf("\nInput of Second Number:%d",y);
System.out.printf("\nInput of Third Number:%d",z);
double sum=printAverage(x,y,z);
System.out.println("\nThe average of the given numbers is:"+sum);
}
public static double printAverage(int x,int y, int z){
double s=(x+y+z)/3;
return s;
}
}
Output:
Enter any three numbers:
3 66 88
Input of first Number:3
Input of Second Number:66
Input of Third Number:88
The average of the given numbers is:52.0

5. Write a Java method to display the middle character of a string.


Note: a) If the length of the string is even there will be two middle
characters.
b) If the length of the string is odd there will be one middle character.
Test Data:
Input a string: 350
Expected Output:
The middle character in the string: 5

import java.util.Scanner;
public class MiddleLetter{
public static void main(String [] args){
Scanner sc=new Scanner (System.in);
System.out.println("Enter any letters:");
String x=sc.next();
displayMiddle(x);
}
public static void displayMiddle(String x){
int length=x.length();
if(length%2==0){
System.out.println(x.charAt(length/2-1)+" "+x.charAt(length/2));
}
else{
System.out.println(x.charAt(length/2));
}

}
}
Output:
Enter any letters:
Subin
b
6. Write a Java method to count all vowels in a string.
Test Data:
Input the string: w3resource
Expected Output:
Number of Vowels in the string: 4

import java.util.Scanner;
public class PrintVowels {
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Input the string: ");
String str = in.nextLine();
System.out.print("Number of Vowels in the string: " +
count_Vowels(str)+"\n");
}
public static int count_Vowels(String str)
{
int count = 0;
for (int i = 0; i < str.length(); i++)
{
if (str.charAt(i) == 'a' || str.charAt(i) == 'e' || str.charAt(i) == 'i'
|| str.charAt(i) == 'o' || str.charAt(i) == 'u')
{
count++;
}
}
return count;
}
}
Output:
Input the string: W3school
Number of Vowels in the string: 2

7. Write a Java method to count all words in a string.


Test Data:
Input the string: The quick brown fox jumps over the lazy dog.
Expected Output:
Number of words in the string: 9
import java.util.Scanner;
public class CountWord{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Input the string: ");
String str = in.nextLine();
System.out.print("Number of Vowels in the string: " + countWords(str)
+"\n");
}
public static int countWords(String str){
int count =1;
for(int i=0;i<str.length()-1;i++){
if((str.charAt(i)==' ')&&(str.charAt(i+1)!=' ')){
count++;
}
}
return count;
}
}
Output:
Input the string: THis is my First program
Number of Vowels in the string: 5

8. Write a Java method to compute the sum of the digits in an integer.


Test Data:
Input an integer: 25
Expected Output:
The sum is 7

import java.util.Scanner;
public class SumOfNumbers {

public static void main(String[] args)


{
Scanner in = new Scanner(System.in);
System.out.print("Input an integer: ");
int digits = in.nextInt();
System.out.println("The sum is " + sumDigits(digits));
}
public static int sumDigits(long n) {
int result = 0;
while(n > 0) {
result += n % 10;
n /= 10;
}
return result;
}
}
Output:
Input an integer: 23
The sum is 5
9. Write a Java method to compute the future investment value at a given
interest rate for a specified number of years.
nt
r
Formula for calculation of future amount= p*(1+ ) , is number of time
n
interest is calculated per unit time t, r is rate in decimal.
Sample data (Monthly compounded) and Output:
Input the investment amount: 1000
Input the rate of interest: 10
Input number of years: 5
Expected Output:
Years FutureValue
1 1104.71
2 1220.39
3 1348.18
4 1489.35
5 1645.31

import java.util.Scanner;
public class CalculateInterest{
public static void main(String [] args){
Scanner sc=new Scanner(System.in);
System.out.println("Enter principle, time, rate respectively:");
double p=sc.nextDouble();
double t=sc.nextDouble();
double r=sc.nextDouble();
System.out.println("Year" +" "+"Future Value");
for(int i=1;i<=t;i++){
double amt=printInterest(p,i,r);
System.out.printf(i +" " +"%.2f\n",amt);
}
}
public static double printInterest(double p, double t, double r){

double amt=p* Math.pow(1+(r/100/12),t);


return amt;

}
}
Enter principle, time, rate respectively:
10000
4
15
Year Future Value
1 10125.00
2 10251.56
3 10379.71
4 10509.45

10.Write a Java method to print characters between two characters (i.e. A


to P ).
Note: Prints 20 characters per line
Expected Output:
()*+,-./0123456789:;
<=>?@ABCDEFGHIJKLMNO
PQRSTUVWXYZ[\]^_`abc
defghijklmnopqrstuvw
xyz
public class MiddleChar {
public static void main(String[] args) {
print_Chars('(', '{',20);
}
public static void print_Chars(char char1, char char2,int n) {
for (int i = 1; char1 <= char2; i++, char1++) {
System.out.print(char1 + " ");
if (i % n == 0) {
System.out.println("");
}
}
System.out.print("\n");
}
}
Output:
()*+,-./0123456789:;
<=>?@ABCDEFGHIJKLMNO
PQRSTUVWXYZ[\]^_`abc
defghijklmnopqrstuvw
xyz{

11.Write a Java method to check whether a year (integer) entered by the


user is a leap year or not.
Expected Output:
Input a year: 2017
false

import java.util.Scanner;
public class LeapYear{
public static void main(String []args){
Scanner input =new Scanner(System.in);
System.out.print("Enter the Year you want to know: ");
int year =input.nextInt();
System.out.println(is_LeapYear(year));
}

public static boolean is_LeapYear(int year)


{
boolean a = (year % 4) == 0;
boolean b = (year % 100) != 0;
boolean c = ((year % 100 == 0) && (year % 400 == 0));

return a && (b || c);


}
}
Output:
Enter the Year you want to know: 2008
true

12.Write a Java method to check whether a string is a valid password.


Password rules:
A password must have at least ten characters.
A password consists of only letters and digits.
A password must contain at least two digits.
Expected Output:
1. A password must have at least ten characters.
2. A password consists of only letters and digits.
3. A password must contain at least two digits
Input a password (You are agreeing to the above Terms and
Conditions.): abcd1234
Password is valid: abcd1234

import java.util.Scanner;
public class PasswordValidity{
public static void main(String [] args){
Scanner sc=new Scanner(System.in);
System.out.println("Before entering the password you must follow
following rules:"+" \nA password must have at least ten characters."
+"\nA password consists of only letters and digits. "
+"\nA password must contain at least two digits." );
System.out.println("Enter your password:");
String password=sc.nextLine();
if (is_Valid_Password(password)) {
System.out.println("Password is valid: " + password);
} else {
System.out.println("Not a valid password: " + password);
}

}
public static boolean is_Valid_Password(String password) {

if (password.length() <10) return false;

int charCount = 0;
int numCount = 0;
for (int i = 0; i < password.length(); i++) {

char ch = password.charAt(i);

if (is_Numeric(ch)) numCount++;
else if (is_Letter(ch)) charCount++;
else return false;
}

return (charCount >= 2 && numCount >= 2);


}

public static boolean is_Letter(char ch) {


ch = Character.toUpperCase(ch);
return (ch >= 'A' && ch <= 'Z');
}

public static boolean is_Numeric(char ch) {

return (ch >= '0' && ch <= '9');


}

}
Output:
Before entering the password you must follow following rules:
A password must have at least ten characters.
A password consists of only letters and digits.
A password must contain at least two digits.
Enter your password:
satyal568
Not a valid password: satyal568

13.Write a Java method (takes a number n as input) to displays an n-by-n


matrix. (0’s and 1’s displayed are random)
Expected Output:
Input a number: 10
1001100011
0010101000
0101000001
1110000111
1101110100
1000110000
0010000111
1101010010
0010000110
1110011110

import java.util.Scanner;

public class MatrixForm {

public static void main(String[] args)


{

Scanner in = new Scanner(System.in);


System.out.print("Input a number: ");
int n = in.nextInt();
printMatrix(n);
}

public static void printMatrix(int n) {


for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
System.out.print((int)(Math.random() * 2) + " ");
}
System.out.println();
}
}
}
Output:
Input a number: 10
0100110000
1111101001
0101110000
0110001011
1000101101
1111001101
1111100100
0111110101
1101011101
0111001011

14.Write Java methods to calculate the area of a triangle.


Expected Output:
Input Side-1: 10
Input Side-2: 15
Input Side-3: 20
The area of the triangle is 72.6184377413890
import java.util.Scanner;
public class SumOfTriangle{
public static void main(String [] args){
Scanner sc=new Scanner (System.in);
System.out.println("Enter three sides of the triangle:");
double x=sc.nextDouble();
double y=sc.nextDouble();
double z=sc.nextDouble();
double area=areaBySides(x,y,z);
System.out.printf("Input-1:%f\n Input-2:%f\n Input-3:%f",x,y,z);
System.out.printf(" \nThe area of the triangle is %.2f",area);
}
public static double areaBySides(double x, double y, double z){
double s=(x+y+z)/2;
double temp= (s*(s-x)*(s-y)*(s-z));
double area=Math.pow(temp,0.5);
return area;
}
}

Output:
Enter three sides of the triangle:
356
Input-1:3.000000
Input-2:5.000000
Input-3:6.000000
The area of the triangle is 7.48

15.Write a Java method to find all twin prime numbers less than 100.
Expected Output: (define a method to check if a number is prime or
not)
(3, 5)
(5, 7)
(11, 13)
(17, 19)
(29, 31)
(41, 43)
(59, 61)
(71, 73)

import java.util.Scanner;
public class TwinPrimeNumber{
public static void main(String [] args){
Scanner sc=new Scanner(System.in);
System.out.println("Enter the nubmber:");
int num=sc.nextInt();
System.out.println("The twin prime number are:");
for (int i = 2; i < 100; i++) {

if (isPrime(i) && isPrime(i + 2)) {


System.out.printf("(%d, %d)\n", i, i + 2);
}
}
}

public static boolean isPrime(int n) {

if (n < 2) return false;

for (int i = 2; i <= n / 2; i++) {

if (n % i == 0) return false;
}
return true;
}
}
Output:
Enter the nubmber:
100
The twin prime number are:
(3, 5)
(5, 7)
(11, 13)
(17, 19)
(29, 31)
(41, 43)
(59, 61)
(71, 73)
16.Write a method that takes an integer as argument and checks if the
number is palindrome or not. The method should return true if the
number is palindrome or false if not. Write a main method to check the
method you have created.
import java.util.Scanner;
public class PlaindromeChecking{
public static void main(String [] args){
Scanner sc=new Scanner (System.in);
System.out.println("Enter any Two numbers:");
int num=sc.nextInt();
if(checkPlaindrome(num)){
System.out.println("Number: "+num+ " is a plaindrome number.");
}
else{
System.out.println("Number: "+num+ " isnot a plaindrome
number.");
}
}
public static boolean checkPlaindrome(int num){
int a, x,y,z,result;
a=num;
x=num%10;
num=num/10;
y=num%10;
num=num/10;
z=num;
result=x*100+y*10+z;
if(a==result){
return true;
}
else{
return false;
}

}
}
Output:
Enter any three digit numbers:
345
Number: 345 isnot a plaindrome number.

17.Write a method named operation that takes two integer arguments and
a char argument, perform the calculation as per the char argument, and
return the result:
Example: operation(2,5,’+’) should return 10 as result.
import java.util.Scanner;
public class Argument{
public static void main(String [] args){
Scanner sc=new Scanner (System.in);
System.out.println("Enter any Two numbers:");
int x=sc.nextInt();
int y=sc.nextInt();
System.out.println("Enter the arguments like (+,-,*,/):”);

char a=sc.next().charAt(0);
double result =operation(x,y,a);
System.out.printf("operation(%d,%d,%s)",x,y,a);
System.out.println(" \nThe result of the operation is "+result);
}
public static double operation(int x,int y, char a){
double result=0;
switch (a){
case '+':
result=x+y;
break;
case '-':
result=x-y;
break;
case '*':
result=x*y;
break;
case '/':
result=x/y;
break;
case '%':
result=x%y;
break;

}
return result;
}
}
Output:
Enter any Two numbers:
47
Enter the arguments like (+,-,*,/):
+
operation(4,7,+)
The result of the operation is 11.0

18. Write a program that uses the function power() to raise a number m to
power n. The function takes integer values for m and n and returns the
result correctly. Use a default value of 2 for n to make the function
calculate squares when this argument is omitted. Write a function
main() to pass the value of m and n and display the calculated result.

import java.util.Scanner;
public class PowerFunc{
public static void main(String [] args){
Scanner sc=new Scanner (System.in);
System.out.println("Enter any Two numbers:");
int m=sc.nextInt();
int n=sc.nextInt();
System.out.println("The power of the number is "+power(m,n));
System.out.println("The power of the number is "+power(m,2));
}
public static double power(int m,int n){
double result= Math.pow(m,n);
return result;
}
public static double power(int m){
return power(m,2);
}
}
Output:
Enter any Two numbers:
46
The power of the number is 4096.0
The power of the number is 16.0

19.Write a method that reverses the string passed as argument.


import java.util.Scanner;
public class ReverseString{
public static void main(String [] args){
Scanner sc=new Scanner (System.in);
System.out.println("Please Enter any word:");
String s=sc.nextLine();
String r=reverse(s);
System.out.println(r);
}
public static String reverse(String s){
char [] a=new char[s.length()];
int letterIndex=0;
for (int i=s.length()-1;i>=0;i--){
a[letterIndex++]=(s.charAt(i));
}
String result="";
for (int i=0;i<s.length();i++){
result+=a[i];
}
return result;
}

}
Output:
Please Enter any word:
Sunway
yawnuS

20.Write a method that replaces all the vowels of a string passed as


argument with the next character.

import java.util.Scanner;
public class VowelReplacement{
public static void main(String [] args){
Scanner sc=new Scanner(System.in);
System.out.println("Enter any word:");
String s=sc.nextLine();
String result=replaceVowel(s);
System.out.println(result);
}
public static String replaceVowel(String s){
char ch;
String s1="";
for(int i=0;i<s.length();i++)
{
ch=s.charAt(i);
int a=ch;
if(ch=='a'||ch=='A'||ch=='e'||ch=='E'||ch=='i'||ch=='I'||ch=='o'||ch=='O'||
ch=='U'||ch=='u')
{
ch=(char)(a+1);
s1+=ch;
}
else
{
s1+=ch;
}
}
return s1;
}
}

Output:
Enter any word:
Red bull is best enegry drink
Rfd bvll js bfst fnfgry drjnk

21.Write a method that accepts two strings as an argument, str1 and str2,
and checks whether if str2 is substring of str1 or not.

import java.util.Scanner;
public class Substring{
public static void main (String []args){
Scanner sc=new Scanner(System.in);
System.out.println("Enter the two string:");
String a=sc.nextLine();
String b=sc.nextLine();
if(checkSubstring(a,b)==true){
System.out.println("The String b is substring of a.");
}
else{
System.out.println("The String b is not substring of a.");
}

}
public static boolean checkSubstring(String a, String b){
for(int i=0;i<a.length();i++){
for(int j=0;j<b.length();j++){
if(a.charAt(i)==b.charAt(j)){
return true;
}
else{
return false;
}
}
}
return false;
}
}
OutPut:
Enter the two string:
Subin Satyal
Sat
The String b is substring of a.

You might also like