0% found this document useful (0 votes)
10 views9 pages

Java Programming Fundamentals Guide

The document contains examples of Java code demonstrating basic programming concepts like variables, data types, operators, conditional statements, loops, functions, arrays, and string manipulation. The code snippets show how to use concepts like if-else, for loops, while loops, switch statements, methods, arrays, finding sum/average/max/min of arrays, sorting arrays, and string operations in Java. Examples include programs to print strings, check if a number is even/odd/prime, find the factorial of a number, reverse an integer, and manipulate 2D arrays.

Uploaded by

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

Java Programming Fundamentals Guide

The document contains examples of Java code demonstrating basic programming concepts like variables, data types, operators, conditional statements, loops, functions, arrays, and string manipulation. The code snippets show how to use concepts like if-else, for loops, while loops, switch statements, methods, arrays, finding sum/average/max/min of arrays, sorting arrays, and string operations in Java. Examples include programs to print strings, check if a number is even/odd/prime, find the factorial of a number, reverse an integer, and manipulate 2D arrays.

Uploaded by

group13glproject
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Language Basics

Q1 )------

public class Main{


public static void(String[] args) {
[Link](args[0] + " Technologies " + args[1]);
}
}

Q2)-------

public class Main{


public static void(String[] args) {
[Link]("Welcome " + args[0]);
}
}

Q3)-------

public class Main{


public static void(String[] args) {
[Link]("The sum of " + args[0] + " and " + args[1] + "is "
+ ([Link](args[0]) + ([Link](args[1])));;
}
}

Flow Control Statements

Q1)--------

a.-----

import [Link];
public class Main
{
public static void main(String[] args) {
Scanner obj = new Scanner([Link]);
int number = [Link]();
if (number == 0) [Link]("Zero");
else if (number < 0) [Link]("Negative");
else [Link]("Positive");
}
}

b.------

import [Link];
public class Main
{
public static void main (String[]args)
{
Scanner obj = new Scanner ([Link]);
String num1 = [Link]();
String num2 = [Link]();
[Link]([Link](num1));

}
}

Q2)-------

import [Link];
public class Main
{
public static void main (String[]args)
{
Scanner obj = new Scanner ([Link]);
int numb = [Link]();
if (numb % 2 == 1) [Link]("Odd");
else [Link]("Even");
}
}

Q5)------

import [Link];
public class Main
{
public static void main (String[]args)
{
Scanner obj = new Scanner ([Link]);
char word = [Link]().charAt(0);
if (word >= 48 && word < 58) [Link]("Digit");
else if ((word >= 65 && word < 91) || (word >= 97 && word <123))
[Link]("Alphabet");
else [Link]("Special Character");
}
}

Q8)------

import [Link];
public class Main
{
public static void main (String[]args)
{
Scanner obj = new Scanner ([Link]);
char word = [Link]().charAt(0);
String comedy = "Nev evaru";
switch(word){
case 'R':
comedy = "Red";
break;
case 'B':
comedy = "Blue";
break;
case 'G':
comedy = "Green";
break;
case 'O':
comedy = "Orange";
break;
case 'Y':
comedy = "Yellow";
break;
case 'W':
comedy = "White";
break;
default:
comedy = "Invalid Code";
}
[Link](comedy);
}
}

Q9)-------

import [Link];
public class Main
{
public static void main (String[]args)
{
// To receive a number and print the corresponding month name
Scanner obj = new Scanner([Link]);
int numb = [Link]();
String name = "";
switch(numb) {
case 1:
name = "January";
break;
case 2:
name = "Febuary";
break;
case 3:
name = "March";
break;
case 4:
name = "April";
break;
case 5:
name = "May";
break;
case 6:
name = "June";
break;
case 7:
name = "July";
break;
case 8:
name = "August";
break;
case 9:
name = "September";
break;
case 10:
name = "October";
break;
case 11:
name = "November";
break;
case 12:
name = "December";
break;
default:
name = "Invalid month";
break;
}
[Link](name);
}
}

Q10)------

import [Link];
public class Main
{
public static void main (String[]args)
{
// 1 to 10 numbers in a single row with one tab space
for (int x = 1; x < 11; x++) {
[Link](x + " ");
}
}
}

Q11)------

import [Link];
public class Main
{
public static void main (String[]args)
{
Scanner obj = new Scanner ([Link]);
for(int x = 23; x < 57; x++){
if (x % 2 == 0) [Link](x);
}
}
}

Q12)------

import [Link];
public class Main
{
public static void main (String[]args)
{
Scanner obj = new Scanner ([Link]);
int numb = [Link]();
boolean flag = true;
for(int x = 2; x < numb; x++){
if (numb % x == 0){
flag = false;
break;
}
}
if (flag) {[Link]("Prime");}
else [Link]("Not a Prime");
}
}

Q13)------

import [Link];
public class Main
{
public static void main (String[]args)
{
// prime numbers between 10 and 99
boolean flag = true;
for(int x = 10; x < 99; x++){
flag = true;
for(int y = 2; y < x; y++){
if (x % y == 0) {
flag = false;
break;
}
}
if (flag) [Link](x);
}
}
}

Q14)------

import [Link];
public class Main
{
public static void main (String[]args)
{
Scanner obj = new Scanner([Link]);
int num = [Link]();
int total = 0;
int rem = 0;
while(num > 0){
rem = num % 10;
total += rem;
num = num / 10;
}
[Link](total);
}
}

Q15)------

import [Link];
public class Main
{
public static void main (String[]args)
{
Scanner obj = new Scanner([Link]);
// 15 Floyds Format
int numb = [Link]();
for(int x = 0; x < numb; x++){
for(int y = 0; y <= x; y++){
[Link]("* ");
}
[Link]("\n");
}
}
}

Q16)------

import [Link];
public class Main
{
public static void main (String[]args)
{
Scanner obj = new Scanner([Link]);
int num = [Link]();
int reverse = 0, rem;
while (num != 0){
rem = num % 10;
reverse = reverse * 10 + rem;
num /= 10;
}
[Link](reverse);
}
}

Arrays

Q1)------

import [Link];
public class Main
{
public static void main (String[]args)
{
// sum and average of the array
int[] arr = {1, 2, 3, 4, 5, 6};
int sum = 0;
for(int i: arr){
sum += i;
}
double avg = sum / [Link];
[Link](sum);
[Link](avg);
}
}

Q2)------

import [Link];
public class Main
{
public static void main (String[]args)
{
// minimum and maximum of the array
int[] arr = {1, 16, 0, -4, 5, 6};
int min = arr[0];
int max = arr[0];
for(int i: arr){
if (min > i) min = i;
if (max < i) max = i;
}
[Link]("Minimum " + min);
[Link]("Maximum " + max);
}
}

Q3)------

import [Link];
public class Main
{
public static void main (String[] kukaBiriyani)
{
// given number is present in the array or not
Scanner obj = new Scanner([Link]);
int[] arr = {1, 16, 0, -4, 5, 6};
int searchItem = [Link]();
int present = -1;
for(int x = 0; x < [Link]; x++) {
if (arr[x] == searchItem){
present = x;
break;
}
}
[Link](present);
}
}

Q4)------

import [Link];
public class Main
{
public static void main (String[] entiMawa)
{
// given number is present in the array or not
Scanner obj = new Scanner([Link]);
int[] arr = {91, 116, 90, 64, 75, 126};
for(int x = 0; x < [Link]; x++) {
[Link]((char)arr[x] + " ");
}
}
}

Q7)-------

import [Link];
public class Array7 {
public static void main(String[] args) {
int a[] = {12,15,22,5,1,6,1};
int m = [Link];
[Link](a);
int temp[] = new int[m];
int j = 0;
for(int i=0;i<m-1;i++) {
if(a[i]!=a[i+1]) {
temp[j++]=a[i];
}
}
temp[j++] = a[m - 1];
for (int i=0;i<[Link]-1; i++) { [Link](temp[i]+" ");
}
}
}

Q10)------

import [Link];
public class Main
{
public static void main (String[] privyet)
{
// even odd
int[] evOd = {3, 3, 2};
int[] evenOdd = new int[[Link]];
int countE = 0;
int countO = [Link] - 1;
for(int x = 0; x < [Link]; x++){
if (evOd[x] % 2 == 0) {
evenOdd[countE] = evOd[x];
countE++;
} else {
evenOdd[countO] = evOd[x];
countO--;
}
}
for(int i: evenOdd){
[Link](i+ " ");
}
}

Q14)------

import [Link];
public class Main
{
public static void main (String[] thinavaRa)
{
// 3 * 3 array
if ([Link] < 9) [Link]("Please enter 9 integer numbers");
else{
[Link]("The given array is:");
int count = 0;
for(int x = 0; x < 3; x++){
for(int y = 0; y < 3; y++){
[Link](thinavaRa[count] + " ");
count++;
}
[Link]("\n");
}
int max = [Link](thinavaRa[0]);
for(int z = 0; z < [Link]; z++){
int kd = [Link](thinavaRa[z]);
if(max < kd) max = kd;
}
[Link]("The biggest number in the given array is " + max);
}
}
}

Common questions

Powered by AI

Data type parsing is crucial in converting string inputs into numerical data types, enabling mathematical operations within the program. The process is necessary because command line arguments and user inputs default to strings. For computations like addition or condition checking, as illustrated with Integer.parseInt in Source 1 Q3, parsing ensures correct arithmetic operations .

The program utilizes a switch-case statement to associate integer inputs with specific month names. Each case corresponds to a month, and when a matching integer is provided via user input, the appropriate month name is printed. A default case handles invalid inputs by printing "Invalid month" .

Loops facilitate the aggregation of data by iterating over arrays, enabling operations like summing values and counting elements. In the examples provided, a for-each loop traverses the array to accumulate the total sum, which is then divided by the number of elements to calculate the average. This concise iteration prevents errors and simplifies complex aggregation processes .

Modularity in Java programs allows the separation of specific functions or tasks into reusable, self-contained units. This approach simplifies complexity, enabling incrementally complex tasks like printing number sequences to be isolated within loops or functions. These isolated components can be easily modified or expanded, exemplifying modularity in the efficient management of tasks such as continuous number printing with specific patterns .

Flow control mechanisms such as loops and conditionals allow continuous processing and manipulation of streams of input data, enhancing operations like counting digits or reversing numbers. Through structured repetition and dynamic variable adjustments, these controls efficiently process each unit of input data to achieve the desired transformation or calculation, as seen in number reversing .

Command line arguments allow a Java program to be more versatile by enabling users to input different data without changing the code. In the provided examples, they are used to personalize outputs and perform operations like summing two numbers. For instance, Source 1 Q3 shows how integers are parsed from command line arguments to calculate their sum .

The logical structure used to differentiate between input types is a sequence of conditional checks (if-else statements). The program determines whether the character falls within specific ASCII ranges: digits (48-57), alphabets (65-90 for uppercase and 97-122 for lowercase), or defaults to special characters if none of these conditions apply .

Switch-case structures improve readability and efficiency, especially in cases with many possible discrete outcomes, like in color classification. They replace the need to evaluate multiple conditions sequentially with a single lookup operation. This reduces potential errors and clarifies logic by grouping all related cases, as seen in the program that assigns colors based on character input .

Iteration enables the program to methodically check all numbers within a specified range if they satisfy the prime condition. The use of nested loops allows comprehensive division checks for each number, confirming no divisors exist other than 1 and the number itself within this range, thus identifying primes effectively. This approach is flexible and can be adapted to different ranges by altering loop boundaries, as shown in Source 2 Q13 .

The structure of arrays in Java allows sequential access to each element, making it straightforward to iterate through all items to compare values. By initializing both 'min' and 'max' with the first element of the array, the program iterates through the array, updating these variables as it processes each element. This efficient traversal ensures all elements are considered for minimum and maximum value calculations .

You might also like