Posts

Showing posts with the label tough

USER INPUT PRINT ORIGINAL,REVERSE ARRAY AND SORTED ARRAY

  package com.cnc ; import java.io.OutputStream ; import java.util.Scanner ; public class Main { public static void main ( String [] args) { Scanner sc = new Scanner( System . in ); System . out .println( "How many values do you want?" ); int count = sc .nextInt(); int [] array = new int [ count ]; for ( int i = 0 ; i < count ; i++) { System . out .println( "Enter array element " + (i + 1 )); array [i] = sc .nextInt(); } System . out .println( "Original element " ); for ( int i = 0 ; i < count ; i++) { System . out .print( array [i] + " " ); } System . out .println( "" ); System . out .println( "Reversed element " ); for ( int i = count - 1 ; i >= 0 ; i--) { System . out .print( array [i] + " " ); } //////////////// SORTED ARRAY /////////////////

FROM USER INPUT ARRANGE IT

Arrange 122,22,43,56,10 ---> 10,22,43,56,122 FROM USER INPUT package com.cnc ; import java.util.Scanner ; public class Main { public static void main ( String [] args) { int count , temp; //User inputs the array size Scanner scan = new Scanner( System . in ); System . out .print( "Enter number of elements you want in the array: " ); count = scan .nextInt(); int num [] = new int [ count ]; System . out .println( "Enter array elements:" ); for ( int i = 0 ; i < count ; i++) { num [i] = scan .nextInt(); } scan .close(); for ( int i = 0 ; i < count ; i++) { for ( int j = i + 1 ; j < count ; j++) { if ( num [i] > num [j]) { temp = num [i]; num [i] = num [j]; num [j] = temp; } } } System . out

CYLINDER CHALLENGE

 1 Write a class with the name Circle.. the class needs one field (instance variable) with name radius of type double.   The class needs to have one constructor with a parameter radius of type double and it needs to initialize the fields. in case the radius parameter is less than 0 it needs to set the radius field value to 0. Write the following methods(instance method): the method named getRadius without any parameters, it needs to return the value of the radius field.  ''        ''          ''       getArea          ''        ''           ''         , ''     ''      ''     ''       ''    calculated area (radius*radius*PI) for PI use Math.PI constant.   2 Write the name Cylinder that extends Circle class. The class needs one field (instance variable) with the name height of type double. The class needs to have one constructor with two parameters radius and height of both of type double. It needs

COMPLEX NUMBER

  package com.cnc ; public class ComplexNumber { private double real ; private double imaginary ; public ComplexNumber ( double real, double imaginary){ this . real = real; this . imaginary = imaginary; } public double getReal (){ return real ; } public double getImaginary (){ return imaginary ; } public void add ( double real, double imaginary){ this . real += real; this . imaginary += imaginary; } public void add ( ComplexNumber complex){ add(complex. real , complex. imaginary ); } public void subtract ( double real, double imaginary){ this . real -= real; this . imaginary -= imaginary; } public void subtract ( ComplexNumber complex){ subtract(complex. real , complex. imaginary ); } }

LARGEST PRIME FACTOR OF A NUMBER

package com.cnc ; public class Main { public static void main ( String [] args) { System . out .println( getLargestPrime ( 20 )); System . out .println( getLargestPrime ( 7 )); } public static int getLargestPrime ( int number){ int i,stored=number; if (number<= 0 ) return - 1 ; else if ( isPrime (number)){ number=stored; } else { for ( i = 2 ; i < number; i++){ if (number%i== 0 ){ stored=i; } } } return stored; } public static boolean isPrime ( int n){ for ( int j = 2 ; j <n; j ++){ if (n% j == 0 ) return false ; else return true ; } return false ; } } ☑ Here i created a new method isTrue. if the number is prime then the number will return the same as output. but if it is not a prime then it will return the largest number excluding itself.

GREATEST COMMON DIVISOR

  package com.cnc ; public class Main { public static void main ( String [] args) { System . out .println( getGreatestCommonDivisor ( 15 , 12 )); } public static int getGreatestCommonDivisor ( int first, int second){ int divisor = 0 ; if ((first < 10 ) || (second < 10 )){ return - 1 ; } if (first > second){ for ( int i= 2 ; i < first; i++){ if ((first % i == 0 ) && (second % i == 0 )){ divisor = i; } } } if (first < second){ for ( int i= 2 ; i < second; i++){ if ((first % i == 0 ) && (second % i == 0 )){ divisor = i; } } } return divisor; } }

NUMBER PALINDROME CHALLENGE

  package com.cnc ; public class Main { public static void main ( String [] args) { System . out .println( isPalindrome ( 101 )); } public static boolean isPalindrome ( int number) { int reverse = 0 ; int temporaryNumber =number; while (number!= 0 ){ reverse = reverse* 10 + number% 10 ; number = number/ 10 ; } if (reverse== temporaryNumber ) return true ; else return false ; } }

DIGIT SUM CHALLENGE

 Write a method with the name sumDigits that have one int parameter called number. If the parameter is >=10 then the method should process the number and return the sum of all digits, otherwise, return -1 to indicate an invalid value. The numbers from 0-9 have 1 digit so we don't want to process them, also we don't want to process negative numbers, so also return -2 for negative numbers. For example, calling the method sumDigit(125) should return 8 since 1+2+5 = 8. Calling the method sumDigit(1) should return -1 as per the requirements described above. Add some codes to the main method to test out the sumDigits method to determine that it is working correctly for valid and invalid values passed as arguments. HINT  Use n%10 to extract the least-significant digit.  Use n=n/10 to discard the least-significant digit  The method needs to be static like other methods so far in the course. Soln: package   com.cnc ; public class Main { public static void main ( String [] args) {

FOR LOOP WITH CHALLENGES

Image
FOR LOOP   Syntax :   for ( int i= 0 ; i< 6 ; i++){                     System . out .println( "Loop " + i + " hello" );                     }

NUMBER OF DAYS IN A MONTH

 Write a method called isLeapYear with a parameter of type int named year . The parameter needs to be greater than or equal to 1 and less than and equal to 9999. If the parameter is not in that range return false. Otherwise, if it is in the valid range, calculate if the year is a leap year and return true if it is, otherwise returns false. [A year is a leap year if it is divisible by 4 not by 100, or it is divisible by 400] Write another method getDaysInMonth with two parameters month and years. Both of type int If parameter month is < 1 or > 12 return -1 If parameter year is < 1 or > 9999 return -1 This method needs to return the number of days in month. be careful about leap years (29 days in Feb) [Use switch statements] Soln   package com.cnc ; public class Main { public static void main ( String [] args) { System . out .println( isLeapYear (- 2000 )); System . out .println( getDaysInMonth (- 2 , 2000 )); } public static boolean isLeapYear ( i

Decimal Comparator

 Write a method areEqualByThreeDecimalPlaces with two parameters of double The method should return boolean and it needs to return true if two double decimal numbers are the same up to three decimal places. Otherwise, return false Example         areEqualByThreeDeciamlPlaces(-3.125,-3.1259)-----> true           areEqualByThreeDeciamlPlaces(-3.125,-3.139)----> false Soln:     package com.cnc ; public class Main { public static void main ( String [] args) { System . out .println( areEqualByThreeDecimalPlaces (- 3.125 , - 3.1259 )); } public static boolean areEqualByThreeDecimalPlaces ( double firstNumber, double secondNumber) { if (( int ) (firstNumber * 1000 ) == ( int ) (secondNumber * 1000 )) { return true ; } return false ; } }