Posts

Showing posts with the label challenge

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 /////////////////

REVERSE ARRAY

package com.cnc ; import java.util.Scanner ; public class Main { public static void main ( String [] args) { Scanner sc = new Scanner( System . in ); System . out .println( "HOW MANY ELEMENT YOU WANT?" ); int n = sc .nextInt(); int [] element = new int [ n ]; for ( int i = 0 ; i < n ; i++) { System . out .println( "ENTER ARRAY ELEMENT " + (i + 1 )); element [i] = sc .nextInt(); } System . out .println( "ORIGINAL ELEMENT " ); for ( int i = 0 ; i < n ; i++) { System . out .print( element [i] + " " ); } System . out .println( " \n REVERSED ELEMENT " ); for ( int i = n - 1 ; i >= 0 ; i--) { System . out .print( element [i] + " " ); } } package com.cnc ; public class Main {   public static void main ( String [] args) { int [ ] array = { 0 , 1 , 2 , 3 , 4 , 5 };

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

ENCAPSULATION CHALLENGE

package com.cnc ; public class Main { public static void main ( String [] args) { Printer printer = new Printer( 50 , true ); System . out .println( "Initial Page count is " + printer .getNumberOfPages()); int pagesPrinted = printer .getNumberOfPages( 4 ); } } package com.cnc ; public class Printer { private int tonerLevel = 100 ; private int numberOfPages ; private boolean isDuplex ; public Printer ( int tonerLevel, boolean isDuplex) { if (tonerLevel>=- 1 && tonerLevel<= 100 ) { this . tonerLevel = tonerLevel; } else { this . tonerLevel =- 1 ; } this . isDuplex = isDuplex; this . numberOfPages = 0 ; } public int addToner ( int tonerAmount){ if (tonerAmount> 0 && tonerAmount<= 100 ){ if ( this . tonerLevel +tonerAmount > 100 ){ return - 1 ; } this . tonerLevel += tonerAmount

POOL AREA

 The Swimming Company has asked you to write an application that calculates the volume of the cuboid-shaped pools. 1 Write a class name Rectangle with two variables width and length of type double. The class needs to have one constructor with parameters width and length both of type double and it needs to initialize the fields.  In case the width parameter is less than 0 it needs to set the width field value to 0 same for the length. Write the following method Method named getWidth without parameters, it needs to return the value of the width field. Method named getLength without parameters, it needs to return the value of the length field. Method named getArea without parameters, it needs to return the value of the area field. 2 Write a class with a name cuboid that extends the Rectangle class. The class needs one field(instance variable) with the name height of typ[e double. The class needs to have one constructor with three parameters width, length, and height all of type double. It

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

INSTANCE CHALLENGE

  Challenge.  start with a base class of a Vehicle, then create a Car class that inherits from this base class.  Finally, create another class, a specific type of Car that inherits from the Car class.  you should be able to handle steering, change gears, and moving (speed in other words).  you will want to decide where to put the appropriate state and behaviors (fields and method).  as mentioned above, changing gears, increasing or decreasing speed should be included.  for your specific type of vehicle, you will want to add something specifically for that type of car. Soln: main package com.cnc ; public class Main { public static void main ( String [] args) { BMW bmw = new BMW( 36 ); bmw .accelerate( 30 ); } } vehicle package com.cnc ; public class Vehicle { private String name ; private String size ; private int currentVelocity ; private int currentDirection ; public Vehicle ( String name, String size) { this . name = name; this . size

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 ); } }

CONSTRUCTOR CHALLENGE

MY APPROACH(BUGS) public class Floor { private double length ; private double width ; public double getLength (){ if ( length < 0 ) return 0 ; else return length ; } public double getWidth (){ if ( width < 0 ) return 0 ; else return width ; } public void setLength ( double length){ this . length = length; } public void setWidth ( double width){ this . width = width; } public double getArea (){ return ( length * width ); } public class Carpet { private double cost ; public double getCost (){ if ( cost < 0 ) return 0 ; else return cost ; } public void setCost ( double cost){ this . cost = cost; } } public class Calculator { private Floor floor ; private Carpet carpet ; public void setTotalCost (){ this . floor = floor ; this . carpet = carpet ; } public double getTotalCost () { return ( fl

CLASS CHALLLENGE

 Create a new class for a bank account. Create fields for the account number, balance, customer name,  email, and phone number. Create getters and setters for each field. Create two additional methods. 1. To allow the customer to deposit funds ( this should increment the balance field). 2. To allow the customer to withdraw funds ( this should decrement the balance field). but not allow the withdraw to complete if there are insufficient funds. You will want to create various code in the Main class ( the one created by IntelliJ) to confirm your code is working or not package com.cnc ; public class BankAccount { private long number ; private long balance ; private String customerName ; private String email ; private long phone ; public void deposit ( double depositAmount){ this . balance += depositAmount; System . out .println( "Deposit of " +depositAmount+ " made. New balance is " + balance ); } public void withdrawal (

USER INPUT CHALLENGES

 Read 10 numbers from the console entered by the user and print the sum of those numbers. create a scanner use hasNextInt() method from the scanner to check if the user has entered an int value if hasNextInt() returns false, print the message "Invalid Number".Continue reading until you have 10 numbers. Use the nextInt() method to get the number and add it to the sum be4 the user enters each num, print the message "Enter number #x:" where x represents the count i.e 1,2,3, etc For eg the first message printed to the user would be "Enter number #1:", the next "Enter number #2", etc. hint use while loop. use a counter variable for counting valid num. close the scanner if you don't need it anymore. Soln package com.cnc ; import java.util.Scanner ; public class Main { public static void main ( String [] args) { Scanner scanner = new Scanner( System . in ); int counter = 0 ; int sum = 0 ; while ( true ){

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.

FLOUR PACK PROBLEM

 Write a method called canPack with three parameters of type int named bingCount, smallCount, goal. The parameter bigCount represents the count of a big bag of flour bags(5Kg) each. The parameter smallCount represents the count of a small bag of flour bags(1Kg) each. The parameter goal represents the goal amount of kilos of flour needed to assemble a package. therefore the sum of kilos bigCount and smallCount must be equal to the value of the goal. the method should true if it is possible to make a package with goal kilos of flour. If the sum is greater than the goal, ensure that only full bangs are used toward the goal amount. if any negative parameter is used return negative. package com.cnc ; public class Main { public static void main ( String [] args) { System . out .println( canPack ( 2 , 0 , 6 )); System . out .println( canPack ( 1 , 0 , 5 )); } public static boolean canPack ( int bigCount, int smallCount, int goal){ if (bigCount * 5 + smallCo

CHECK IF THE NUMBER IS PERFECT OR NOT

 EXAMPLES ----> 6 has factor 3,2,1 and 6. But a number is perfect when it is equal to the sum of its proper divisor excluding the number itself  as 6 = 3+2+1 package com.cnc ; public class Main { public static void main ( String [] args) { System . out .println( isPerfectNumber ( 28 )); } public static boolean isPerfectNumber ( int number) { int sum = 0 ; if (number < 1 ) return false ; else for ( int a = 1 ; a < number; a++) { if ((number % a == 0 )) { sum = sum +a; } if (sum==number) return true ; } return false ; } }   

FIND FACTORS OF NUMBERS

  package com.cnc ; public class Main { public static void main ( String [] args) { printFactors ( 15 ); } public static void printFactors ( int number) { if (number < 1 ) System . out .println( "Invalid Value" ); else for ( int a = 1 ; a <= number; a++) { if ((number % a == 0 )) { System . out .println(a); } } } }

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; } }