Posts

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

Arrays

Image
In computer programming, an array is a collection of similar types of data. For example, if we want to store the names of 100 people then we can create an array of the string type that can store 100 names. String[] array = new String[ 100 ]; The number of values in the Java array is fixed. That is, the above array can not store more than 100 elements. How to declare an array in Java? In Java, here is how we can declare an array. dataType[] arrayName; dataType  - it can be  primitive data types  like  int ,  char ,  double ,  byte , etc. or  Java objects arrayName  - it is an  identifier For example, double [] data; Here,  data  is an array that can hold values of type  double . But, how many elements can array this hold? Good question! To define the number of elements that an array can hold, we have to allocate memory for the array in Java. For example, // declare an array double [] data; // allocate memory data = new Double[ 10 ]; Here, the array can store  10  elements. We can a

POLYMORPHISM

package com.cnc ; class Movie { private String name ; public Movie ( String name) { this . name = name; } public String script () { return "The plot is here" ; } public String getName () { return name ; } } class Jollywood extends Movie { public Jollywood () { super ( "Assamese" ); } public String script () { return "jai aai Axom" ; } } class JatinBora extends Jollywood { public JatinBora (){ } public String script () { return "ratnakar" ; } } class Bollywood extends Movie { public Bollywood () { super ( "Hindi" ); } public String script (){ return "Jai hind!" ; } } class Sharukh extends Bollywood { public Sharukh (){ } public String script () { return "hehe" ; } } public class Main { public static void main ( String [] args) { for ( int i= 1 ; i&l

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