Review on Arrays
1. One Dimensional Arrays Practice I
import java.util.Scanner;
class Main {
public static void main(String[] args) {
int[] nums = new int[]{
-2, 1, 1, 2, 3,
9, 10, -3, 100, 2,
4, -5, 1, 10, -100,
5, 2, 3, -1, 1,
-3, -4, -5, -6, -7,
9, 9, 9, 9, 9,
1, 2, 3, 4, 5,
100, 100, 100, 100, 100
};
for(int i = 0; i<40; i++){
if(nums[i]>0){
nums[i]=nums[i]*nums[i]*nums[i];
System.out.println(nums[i]);
} else if(nums[i]<0){
nums[i]=nums[i]*nums[i];
System.out.println(nums[i]);
}
}
}
}
2. One Dimensional Arrays Practice II
import java.util.Scanner;
class Main {
public static void main(String[] args) {
double[] nums = new double[]{1.4, 1.2054, 2.2, 2.5, 3.66, 3.0, 4.024,
4.00001, 5.5, 5.10};
for(int i = 10-1; i>=0; i--){
System.out.format("%.2f\n", nums[i]);
}
}
}
1. Multi Dimensional Arrays Practice l
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter # of rows: ");
int row = in.nextInt();
System.out.print("Enter # of columns: ");
int col = in.nextInt();
int[][] arr = new int[row][col];
System.out.println("Elements:");
for(int i = 0; i<row; i++){
for(int j=0; j<col; j++){
arr[i][j]=in.nextInt();
}
}
System.out.print("Enter the boogeyman's value: ");
int bog = in.nextInt();
for(int i = 0; i<row; i++){
for(int j=0; j<col; j++){
if(arr[i][j]==bog){
int holderI = i+1, holderJ=j+1;
System.out.println("BOOGEYMAN LOCATED AT ROW "+holderI+", COLUMN
"+holderJ+"!");
}
}
}
}
}
2. Multi Dimensional Arrays Practice Il
// NAAY SAYOP
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter # of rows: ");
int row = in.nextInt();
System.out.print("Enter # of columns: ");
int col = in.nextInt();
int[][] arr = new int[row][col];
System.out.println("Enter elements:");
for(int i = 0; i<row; i++){
for(int j=0; j<col; j++){
arr[i][j]=in.nextInt();
}
}
int count=0;
for(int i = 0; i<row; i++){
int sum=0;
for(int j=0; j<col; j++){
sum=sum+arr[i][j];
}
if(sum%2==0){
count++;
break;
}
}
System.out.print("Even row: "+count);
}
}
1. Array Traversal Practice I
class Main {
public static void main(String[] args) {
int[] array = new int[]{1,2,3,4,5};
for(int i =0; i<5; i++){
System.out.format("%d",array[i]);
if(i!=4){
System.out.format(",");
}
}
}
}
2. Array Traversal Practice II
//NAAY SAYOP
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter size: ");
int size = in.nextInt();
int[] array = new int[size];
int minus=0, holdermin;
for(int i =0; i<size; i++){
System.out.format("Enter element %d: ",i+1);
array[i]=in.nextInt();
if(array[i]<=0){
holdermin = size-minus;
size=size-holdermin;
break;
}else {
minus++;
continue;
}
}
for(int i =0; i<size; i++){
if (i==0){
System.out.format("[%d,",array[i]);
} else if(i>0 &&i<size){
System.out.format("%d",array[i]);
}
if(i!=size-1 && i!=0){
System.out.format(",");
} else if(i==size-1){
System.out.format("]");
}
}
}
}
Functions
1. Functions With No Parameters and Return Values Practice ll
wala
2. Functions With No Parameters and Return Values Practice l
import java.util.Scanner;
class Main {
static int called = 0;
public static void hello() {
called++;
System.out.println("Call counter: " + called);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Call count: ");
int n = sc.nextInt();
for(int called = 1; called <= n; called++) {
hello();
}
}
}
1. Functions With No Parameters But With Return Values Practice I
import java.util.Scanner;
class Main {
public static int askInput() {
Scanner scan = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = scan.nextInt();
return number;
}
public static void main(String[] args) {
int total = askInput();
System.out.println(total);
}
}
2. Functions With No Parameters but With Return Values Practice II
import java.util.Scanner;
class Main {
public static int askInput() {
Scanner scan = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = scan.nextInt();
if(number % 2 == 0)
{
System.out.println("Even");
}
else
{
System.out.println("Odd");
}
return number;
}
public static void main(String[] args) {
askInput();
}
}
1. Functions With Parameters and No Return Values Practice l
import java.util.Scanner;
class Main {
public static int palindrome(int number) {
Scanner scan = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = scan.nextInt();
int r,sum=0;
int temp=num;
while(num>0)
{
r=num%10;
sum=(sum*10)+r;
num=num/10;
}
if(temp==sum)
System.out.println(temp+" is a palindrome!");
else
System.out.println(temp+" is not a palindrome!");
return num;
}
public static void main(String[] args) {
palindrome(0);
}
}
2. Functions With Parameters and No Return Values Practice ll
import java.util.Scanner;
class Main {
public static void palindrome()
{
Scanner scan = new Scanner(System.in);
String reverseStr = "";
System.out.print("Enter a string: ");
String str = scan.nextLine();
int strLength = str.length();
for (int i = (strLength - 1); i >=0; --i)
{
reverseStr = reverseStr + str.charAt(i);
}
if(str.toLowerCase().equals(reverseStr.toLowerCase()))
System.out.println(str + " is a palindrome!");
else
System.out.println(str + " is not a palindrome!");
}
public static void main(String[] args) {
palindrome();
}
}
1. Functions With Parameters and Return Values Practice l
import java.util.Scanner;
class Main {
public static int compare(int a, int b){
if(a>b){
return a;
} else if (a<b){
return b;
}
return 0;
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
char a, b;
System.out.print("Enter first character: ");
a = scan.next().charAt(0);
System.out.print("Enter second character: ");
b = scan.next().charAt(0);
int ascii1 = a, ascii2 = b;
int result = compare(ascii1,ascii2);
System.out.print("Result value = " + result);
}
}
2. Functions With Parameters and Return Values Practice ll
import java.util.Scanner;
class Main {
public static int compare(int a, int b, int c){
if(a>=b && a>=c){
return a;
} else if (b>=c && b>=a){
return b;
}else {
return c;
}
//return 0;
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int a, b, c;
System.out.print("Enter number 1: ");
a = scan.nextInt();
System.out.print("Enter number 2: ");
b = scan.nextInt();
System.out.print("Enter number 3: ");
c = scan.nextInt();
int result = compare(a,b,c);
System.out.print("Greatest: " + result);
}
}
1. Recursive Functions Practice l
class Main {
public static void fun(int n) {
if (n > 0) {
fun(n-1);
if(n%2==0){
System.out.print(n+" ");
}
}
}
public static void main(String[] args) {
fun(20);
}
}
2. Recursive Functions Practice ll
import java.util.Scanner;
class Main{
public static void factorial(int num){
int i,fact=1;
for(i=1;i<=num;i++){
fact=fact*i;
}
System.out.println(fact);
}
public static void main(String args[]){
Scanner in = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = in.nextInt();
factorial(num);
}
}
Functions with Arrays
1. Functions with 1D Arrays Practice I
import java.util.Scanner;
class Main {
public static void main(String[] args) {
char[] array = new char[]{
'x','s','q','a','y','i','u','o','p','a','w','q'
};
vowelCount(array);
}
static void vowelCount(char[] array){
int count=0;
for(int i=0; i<array.length; i++){
if(array[i]=='a' || array[i]=='e' || array[i]=='i' || array[i]=='o' ||
array[i]=='u'){
count++;
}
}
System.out.println("Number of vowels: "+ count);
}
}
2. Functions with 1D Arrays Practice II
import java.util.Scanner;
class Main{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.print("Enter size of array: ");
int size = in.nextInt();
int[] arr= new int[size];
for(int i = 0; i<size; i++){
System.out.format("Enter element %d: ", i+1);
arr[i] = in.nextInt();
}
int max = maxArray(arr, size);
System.out.format("Maximum element: %d", max);
}
static int maxArray(int[] arr, int size){
int max=0;
for(int i = 0; i<size; i++){
if(max< arr[i]){
max=arr[i];
}
}
return max;
}
}
1. Functions with 2D Arrays Practice I
class Main {
public static void main(String[] args) {
int[][] array = new int[][]{
{1, 6, 4},
{7, 2, 8},
{5, 9, 3}
};
int row=array.length;
int col=array[0].length;
displayElements(array, row, col);
}
static void displayElements(int[][] arr, int rows, int cols) {
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
System.out.printf("%d ", arr[i][j]);
}
System.out.println();
}
}
}
2. Functions with 2D Arrays Practice ll
import java.util.*;
class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int[][] arr = new int[3][3];
int rows=arr.length;
int cols=arr[0].length;
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
System.out.printf("R%dC%d: ", i+1,j+1);
arr[i][j] = input.nextInt();
}
}
displayElements(arr, rows, cols);
}
static void displayElements(int[][] arr, int rows, int cols) {
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
System.out.printf("%d ", arr[i][j]);
}
System.out.println();
}
}
}