0% found this document useful (0 votes)
222 views16 pages

Java Code Examples for Beginners

This document contains multiple Java code snippets and classes demonstrating different Java programming concepts like object-oriented programming, methods, constructors, inheritance, interfaces, exceptions, arrays, and more. The code covers concepts like method overloading, inheritance, exception handling, array initialization, and more.

Uploaded by

kumar kanishka
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)
222 views16 pages

Java Code Examples for Beginners

This document contains multiple Java code snippets and classes demonstrating different Java programming concepts like object-oriented programming, methods, constructors, inheritance, interfaces, exceptions, arrays, and more. The code covers concepts like method overloading, inheritance, exception handling, array initialization, and more.

Uploaded by

kumar kanishka
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
  • Calculator Class
  • Cafe and Cafeteria Classes
  • Demo Class
  • Morning Class
  • Vehicle Interface and Testing Class
  • Testing Class
  • Activate Class
  • RoomArea and Multiple Classes
  • Inheritance and Test Classes
  • Main and Exception Handling Classes
  • Acc_Byte1 Class
  • SwitchD Class
  • Sacrifice Class

Java Programming

public class Calculator


{
void add(int addd)
{
[Link]("int");
}
void add(float ddb)
{
[Link]("float");
}
void add(double ddda)
{
[Link]("dou");
}
public static void main(String s[])
{
Calculator c=new Calculator();
[Link](12.24);
}
}

public class Condition


{
public static void main(String s[])
{
double var1=3.13;
double var2=3.13;
for(int z=3; z>=3; --z)
{
if((--var2 > 1) && (var1++ >
2))
{
var2=var1+var2;
}
}
[Link](var1 + " " +
var2);
}
}

class Cafe
{
public int cafe=0;
public Cafe(String test){
cafe=9;
}
public Cafe(){ }
}

public class Cafeteria extends Cafe


{
public Cafeteria(String text)
{ cafe=11; }
public static void main(String s[])
{
[Link](new
Cafeteria("test").cafe);
}
}

public class Demo


{
public static void main(String s[])
{
int i=1;
while(i--) //Line 1
{
[Link](i + " ");
}
}
}
public class Morning
{
static String value="testify";
static int value1=25;
static {
value1=50;
[Link](value);
[Link](value1);
}
public static void main(String s[])
{ [Link](value);
}
}
interface Vehicle
{
int noOfWheels;
}
public class Car implements Vehicle
{
public static void main(String s[])
{ [Link]([Link]);
}
}

public class Testing


{
public static void main(String s[])
{ int intx=100;
boolean BVal1=true;
boolean BVal2=false;
if( (intx !=4)&& (intx >=99) || !
BVal1)
[Link]("Accenture");
[Link]("IDC");
BVal2=BVal1;
if( (BVal2=true) && BVal1 !=
BVal2)
[Link]("High
Performance Delivered");
}
}

public class Testing


{
public static void main(String
s[])
{
double value[]=new double
[81];
value[1]=21.23;
[Link]("Arra val
contain" + value[0]);
}
}

public class Activate


{
public static void main(String s[])
{
double
finalResult=captureResultA(37.5);
double
finalValue=captureResultB(37.5);
[Link](finalResult+finalValue);
}
public static float captureResultA(double
value)
{
float output=new Float(value);
return output;
}
public static float captureResultB(double
value)
{
float output=new Float(value);
return output;
}
}

class RoomArea
{ float length;
float breadth;
void getData(float a, float b)
{ length=a;
breadth=b;
}
}
public class Multiple
{ public static void main(String s[])
{ float area;
RoomArea room=new RoomArea();
[Link](15,5);
area=[Link] * [Link];
[Link]("Area: " +
area);
}
}
public class tests
{ int a;
tests()
{
[Link]("I am default
constructor");
}
public static void main(String s[])
{
tests t1=new tests();
}
}

public class MainClass


{ public static void main(String s[])
{ int i=1;
int j=0;
try
{
[Link](i/j);
//line 1
}
catch(Exception e)//block 1
{
[Link]("i am
exception");
}
catch(ArithmeticException
m)//block 2
{
[Link]("i am arith
exception");
}

}
}

class Main
{
public Main()
{ [Link]("Calling ASE");
}

public void show()


{ [Link]("show");
}
}
public class Attention
{
public static void main(String s[])
{
Main the_values[]=new Main[3];
}
}

public class Acc_Byte1


{
public static void main(String s[])
{
int b1=80;
int b2=100;
int b3;
b3=++b1 * b1/100 + ++b2;
[Link]("b3: " +
b3);
}
}
public class SwitchD
{
public static void main(String s[])
{
int i=1;
switch(i){
case 0:
[Link]("zero"); break;
case 1:
[Link]("one");
case 2:
[Link]("two");
default:
[Link]("default");
}
}
}
public class Sacrifice
{
private int
variableA=showOutput();
private int variableB=15;
private int showOutput()
{
return variableB;
}
public static void main(String
s[])
{ [Link]( (new
Sacrifice()).variableA);
}
}

Common questions

Powered by AI

`variableA` is initialized by `showOutput()`, which returns `variableB=15` at declaration. In Java, instance variables are set during declaration or in a constructor, with method-distributed assignment making `variableA=15` due to `variableB=15` assignment in `showOutput`, so it prints 15 .

The Condition class for-loop executes only once since the initial condition `z=3` is already true, and the loop decrements `z` but no check is repeated. Within the loop, `(--var2 > 1)` evaluates to true as `var2` decrements to `2.13`; `(var1++ > 2)` is also true. Therefore, the sum `var2=var1+var2` is executed, resulting in `var2=6.26`. Post-loop, `var1` is 4.13 (after increment) and final output is '4.13 6.26' .

In Java, numeric arrays like the `double value[]` are initialized with default values of `0.0`. Thus, when `System.out.println("Arra val contain" + value[0]);` is executed, it prints 'Arra val contain0.0' since `value[0]` was never assigned any other value, demonstrating Java's initialization mechanism for double arrays .

The `SwitchD` class executes case `1` matched with `i=1`, printing 'one', but lacks break statements, allowing fall-through. Thus, subsequent prints 'two' and 'default' are executed until the switch structure ends. Proper break statements per case would prevent this execution beyond the intended match .

When a new `Cafeteria` object is instantiated with a string, the constructor `Cafeteria(String text)` is invoked, setting the `cafe` attribute to 11. This overrides the default constructor for `Cafe`, which would set `cafe` to 9. Consequently, the printed value for the `cafe` attribute is 11, demonstrating constructor overloading in inheritance .

The first condition `(intx !=4)&& (intx >=99) || ! BVal1` is true as `intx` is 100, leading to 'Accenture' being printed, followed by 'IDC'. `BVal2=BVal1` sets `BVal2` to true. The condition `(BVal2=true) && BVal1 != BVal2` evaluates the assignment, not comparison, setting `BVal2` to true, making `BVal1 != BVal2` false, hence 'High Performance Delivered' is not printed .

The `MainClass` attempts to divide `i` by `j`, where `j=0`, leading to an `ArithmeticException`. However, only the generic `Exception` catch block is defined, capturing the arithmetic exception arising from division by zero. Therefore, 'i am exception' is printed, demonstrating the priority of catch blocks in exception handling .

The Vehicle interface incorrectly declares `int noOfWheels;`, trying to assign a property which is illogical as interfaces can declare methods with implicit public abstract functions, not variables. To fix this, change it to a method, e.g., `int getNoOfWheels();`, and implement this method in `Car` .

The Demo class contains a `while(i--)` loop that starts with `i=1`. However, the predecrement operation makes `i` immediately equal to 0 before the loop body is executed. Since the condition becomes false right at the beginning, the loop body is never executed, resulting in no output .

When the Calculator class executes, the output will be 'dou'. The method `add(double ddda)` is selected over `add(int addd)` and `add(float ddb)` because the parameter passed, `12.24`, is a `double` literal by default. Java chooses the most specific method available that can accept the parameter, and in this case, it is `add(double)` .

Java Programming
public class Calculator 
{ 
void add(int addd)
{
System.out.println("int");
}
void add(float ddb)
{
System.o
for(int z=3; z>=3; --z)
{
if((--var2 > 1) && (var1++ > 
2))  
{
var2=var1+var2;
}
}
System.out.println(var1 + " " + 
var2);
}
public static void main(String s[])
{
System.out.println(new 
Cafeteria("test").cafe);
}
 
}
public class Demo  
{ 
 
public
public class Morning  
{
static String value="testify";
static int value1=25;
static {
value1=50;
System.out.println(value);
interface Vehicle
{
int noOfWheels;
}
public class Car  implements Vehicle
{
public static void main(String s[])
{
System.out
{
public static void main(String s[])
{
int 
intx=100;
boolean BVal1=true;
boolean BVal2=false;
if( (intx !=4)&& (intx >=99)
public static void main(String 
s[])
{
double value[]=new double 
[81];
 value[1]=21.23;
 System.out.println("Arra val 
conta
System.out.println(finalResult+finalValue);
 
}
 
public static float captureResultA(double 
value)
{
float output=new Float(
{
public static void main(String s[])
{
float area;
RoomArea room=new RoomArea();
room.getData(15,5);
area=room.length * room
public class tests 
{
int a;
tests()
{ 
 
   System.out.println("I am default 
constructor");

You might also like