0% found this document useful (0 votes)
19 views14 pages

Mod 2

The document contains code snippets demonstrating various Java programming concepts like: 1) Defining and using methods that take parameters and return values. Methods can modify passed parameters or return new objects. 2) Implementing linear and binary search algorithms to search arrays. 3) Sorting arrays using selection sort. 4) Using control structures like if/else, for loops, while loops, switch statements to control program flow. 5) Declaring and initializing arrays, primitive data types, and using built-in Math functions. In summary, the document shows examples of common Java coding patterns for arrays, searching, sorting, methods and control flow.

Uploaded by

Nandini
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views14 pages

Mod 2

The document contains code snippets demonstrating various Java programming concepts like: 1) Defining and using methods that take parameters and return values. Methods can modify passed parameters or return new objects. 2) Implementing linear and binary search algorithms to search arrays. 3) Sorting arrays using selection sort. 4) Using control structures like if/else, for loops, while loops, switch statements to control program flow. 5) Declaring and initializing arrays, primitive data types, and using built-in Math functions. In summary, the document shows examples of common Java coding patterns for arrays, searching, sorting, methods and control flow.

Uploaded by

Nandini
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

import java.util.

Scanner;

public class SimplelfDeno {

public static void main(String[] args) { Scanner input = new Scanner(System.in): System.out.println("Enter
an integer: "); int number = input.nextInt():

if (number % 5 == 0)

System.out.println("HiFive"):

if (number % 2 == 0)

System.out.println("HiEven"):

if (radius >= 0) {

area = radius radius 3.14159; *

System.out.println("The area for the " + "circle of radius " + radius +

"is" area);

else {

System.out.println("Negative input");
}

if (score >= 90.0)

else if (score >= 80.0)

System.out.print ("A"); System.out.print ("B"); else if (score >= 70.0) System.out.print ("C"); else if (score
>= 60.0) System.out.print ("D"); else System.out.print ("F");

int i = 1, j = 2, k = 3;

if (i > j) if (i > k)

System.out.println("A");

else System.out.println("B");

import java.util.Scanner;

public class SubtractionQuiz (

public static void main(String[] args) (

// 1. Generate two random single-digit integers int number1 = (int) (Math.random() 10);

int number2 = (int) (Math.random() 10);

// 2. If number1 < number2, swap number1 with number2

if (number1 < number2) C


int temp number1;

number1 number2;

number2 temp:

// 3. Prompt the student to answer "what is number1 - number22"

System.out.print

("What is " + number1 + "-" + number2 + "? "); Scanner input = new Scanner(System.in);

int answer input.nextInt();

// 4. Grade the answer and display the result

if (numberl - number2 answer) - System.out.println("You are correct!");

else

System.out.println("Your answer is wrong. \n" + number1 + + number2 +" should be " + (number1 -
number2));

import java.util.Scanner;

public class ComputeAndInterpretBMI { public static void main(String[] args) { Scanner input = new
Scanner(System.in);
// Prompt the user to enter weight in pounds System.out.print ("Enter weight in pounds: "); double
weight = input.next Double();

17 Prompt the user to enter height in inches System.out.print ("Enter height in inches: "); double height
= input.next Double();

final double KILOGRAMS PER POUND = 0.45359237; // Constant final double METERS PER INCH = 0.0254;
//Constant

// Compute BMI

double weight InKilograms weight KILOGRAMS PER POUND;

double height InMeters height METERS PER INCH; double bmi weight InKilograms/

(height InMeters height InMeters);

// Display result

System.out.printf("Your BMI is $5.2f\n", bmi);

if (bmi < 16)

System.out.println("You are seriously underweight");

else if (bmi < 18)

System.out.println("You are underweight");

else if (bmi < 24)


System.out.println("You are normal weight");

else if (bmi < 29) System.out.println("You are overweight");

else if (bmi < 35)

System.out.println("You are seriously overweight");

else

System.out.println("You are gravely overweight");

if (status 0) { ==

// Compute tax for single filers

else if (status == 1) {

// Compute tax for married file jointly

// or qualifying widow(er)

else if (status == 2) {
// Compute tax for married file separately

else if (status == 3) {

// Compute tax for head of household

else {

// Display wrong status

import java.util.Scanner;

public class TestBooleanOperators (

public static void main(String[] args) {

// Create a Scanner

Scanner input new Scanner(System.in);

// Receive an input

System.out.print ("Enter an integer: ");


int number = input.nextInt();

System.out.println("Is +number+" divisible by 2 and 32 " +

((number%2 0) && (number 3 == 0)));

System.out.println("Is "+number+" divisible by 2 or 32 " +

((number 2 == 0) | (number 3 == 0)));

System.out.println("Is " + number +

"divisible by 2 or 3, but not both? " + ((number 2 0) (number 3 == 0)));

import java.util.Scanner; public class Lottery

public static void main(String[] args) { int ran= (int) (Math.random()*100);

System.out.println(ran); int ranl=ran/10;

int ran2=ran%10;

System.out.println(ran1);

System.out.println(ran2);
Scanner Inum=new Scanner(System.in); System.out.println("Enter Lottery Number: ");

int In=lnum.nextInt();

int 1n1-1n/10; int 1n2=1n%10;

if (ran==1n)

System.out.println("Lucky: won 10000$");

elsif ((ran1==1n2)&&(ran2==1n1)) System.out.println("won 3000$");

else if ((ran1==1n1) | | (ran1==1n2) || (ran2==1n1) | | (ran2==1n2) System.out.println("won 1000$");

else

System.out.println("Sorry");

public class Main {

public static void main(String[] args) {

int day = 4;

switch (day) {
case 6:

System.out.println("Today is Saturday");

break;

case 7:

System.out.println("Today is Sunday");

break;

default:

System.out.println("Looking forward to the Weekend");

public class TestBreak

public static void main(String[] args) {

int sum = 0;

int number = 0;

while (number < 20) (


number++;

sum += number;

if (sum >= 100)

break;

System.out.println("The number is " + number); System.out.println("The sum is " + sum);

public class TestContinue ( public static void main(String[] args) {

int sum = 0;

int number = 0;

while (number < 20) (

number++;

if (number == 10 || number == 11)

continue;

sum += number;
System.out.println("The sum is " + sum);

public class Test {

public static void main(String[] args) {

int[] values = new int[5]

for (int i = 1; j<9; i++) {

values[i] = i + values[i-1];

}}

values[0] = values[1] + values[4];

java.util.Scanner input = new java.util.Scanner(System.in); System.out.print("Enter " + myList.length + "


values: "); for (int i = 0; i < myList.length; i++) myList[i] = input.nextDouble();

[28/05, 23:00] Nandini: public class Test {

public static void main(String[] args) {

int x = 1; // x represents an int value int[] y = new int[10]; // y represents an array of int values

m(x, y); // Invoke m with arguments x and y


System.out.println("x is " + x); System.out.println("y[0] is " + y[0]);

public static void m(int number, int[] numbers) { number = 1001; // Assign a new value to number
numbers [0] = 5555; // Assign a new value to numbers [0]

public static int[] reverse (int[] list) { int[] result = new int[list.length]:

for (int i = 0, j = result.length - 1; i list.length; i++, j--)

result[j] = list[i];

return result;

public class Linear Search {

/** The method for finding a key in the list */

public static int linearSearch (int[] list, int key) {

for (int i = 0; i < list.length; i++)

if (key = list [i]) return i;

return -1;
}

/** Use binary search to find the key in the list */ public static int binarySearch (int[] list, int key) (

int low = 0;

int high = list. length - 1;

while (high >= low) {

int mid = (low + high) / 2;

if (key <list [mid])

high mid-1;

else if (key == list [mid])

return mid;

else

low = mid + 1;

return -1 low;

/** The method for sorting the numbers */


public static void selectionSort (double[] list) { for (int i = 0; i < list.length; i++) { // Find the minimum in
the list[i..list.length-1] double currentMin list[i]: int currentMinIndex = 1; for (int j = i + 1; j < list.length; j+
+) { if (currentMin > list [j]) ( currentMin list[j]; currentMinIndex = j;

// Swap list[i] with list [currentMinIndex] if necessary; if (currentMinIndex = i) ( list [currentMinIndex] =


list[i]; list[i] currentMin;

You might also like