Core Java Programming Practise
Thursday, 11 June 2015
Friday, 6 February 2015
parrot is talking means true, if parrot is talking before hour 7 and after 20 then return true. hour limit is between 1 and 23.
import java.util.Scanner;
class Test
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.print("Enetr Boolean Value= ");
boolean talking=sc.nextBoolean();
System.out.print("Enter Hour value = ");
int hour=sc.nextInt();
if(talking && hour<7 || talking && hour>20)
{
System.out.println("true");
}
else
{
System.out.println("false");
}
}
}
class Test
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.print("Enetr Boolean Value= ");
boolean talking=sc.nextBoolean();
System.out.print("Enter Hour value = ");
int hour=sc.nextInt();
if(talking && hour<7 || talking && hour>20)
{
System.out.println("true");
}
else
{
System.out.println("false");
}
}
}
Given an int n, return the absolute difference between n and 21, except return double the absolute difference if n is over 21. diff21(19) → 2 diff21(10) → 11 diff21(21) → 0
import java.util.Scanner;
class Test
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter any Number= ");
int n = sc.nextInt();
if(n<=21)
{
int c=21-n;
System.out.print(c);
}
else
{ int d=n-21;
System.out.print(2*d);
}
}
}
class Test
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter any Number= ");
int n = sc.nextInt();
if(n<=21)
{
int c=21-n;
System.out.print(c);
}
else
{ int d=n-21;
System.out.print(2*d);
}
}
}
Given two int values, return their sum. Unless the two values are the same, then return double their sum. sumDouble(1, 2) → 3 sumDouble(3, 2) → 5 sumDouble(2, 2) → 8
Given two int values, return their sum. Unless the two values are the same, then return double their sum.
sumDouble(1, 2) → 3
sumDouble(3, 2) → 5
sumDouble(2, 2) → 8
Solution:
import java.util.Scanner;
class Test
{
public static void main(String [] args)
{
Scanner sc=new Scanner(System.in);
System.out.print("Enetr First Number= ");
int a=sc.nextInt();
System.out.print("Enetr First Number= ");
int b=sc.nextInt();
if(a==b)
{
int c=2*(a+b);
System.out.println(c);
}
else
{
int d=a+b;
System.out.println(d);
}
}
}
sumDouble(1, 2) → 3
sumDouble(3, 2) → 5
sumDouble(2, 2) → 8
Solution:
import java.util.Scanner;
class Test
{
public static void main(String [] args)
{
Scanner sc=new Scanner(System.in);
System.out.print("Enetr First Number= ");
int a=sc.nextInt();
System.out.print("Enetr First Number= ");
int b=sc.nextInt();
if(a==b)
{
int c=2*(a+b);
System.out.println(c);
}
else
{
int d=a+b;
System.out.println(d);
}
}
}
We have two monkeys, a and b, and the parameters aSmile and bSmile indicate if each is smiling. We are in trouble if they are both smiling or if neither of them is smiling. Return true if we are in trouble. monkeyTrouble(true, true) → true monkeyTrouble(false, false) → true monkeyTrouble(true, false) → false
Q:1 |
|
class Test
{
public static void main(String [] args)
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter bollean value for aSmile= ");
boolean aSmile=sc.nextBoolean();
System.out.print("Enetr only booblean value for bSmile= ");
boolean bSmile=sc.nextBoolean();
if(aSmile && bSmile || !aSmile && !bSmile)
{
System.out.println("true");
}
else
{
System.out.println("false");
}
}
}
Monday, 5 January 2015
Problem : sameFirstLast Element
return true if the array is length 1 or more, and the first element and the last element are equal.
public boolean sameFirstLast(int[] nums){if(nums.length>1)return((nums[0]==nums[nums.length-1]));else if(nums.length==1)return true;elsereturn false;}
Problem Name : commonEnd in java. return true if they have the same first element or they have the same last element
Solution:
Hint: return true if they have the same first element or they have the same last element
public boolean commonEnd(int[] a, int[] b)
{
if(a.length>=1 && b.length >=1)
return ((a[0]==b[0])||(a[a.length-1]==b[b.length-1]));
else
return (false);
}
|
Subscribe to:
Posts (Atom)