0% found this document useful (0 votes)
26 views5 pages

Java Program Examples for Beginners

The document contains 5 code examples that demonstrate different Java programming concepts. The first example checks if a number is prime or not. The second creates a Book class and calls its get method. The third throws a custom exception. The fourth draws shapes using graphics. The fifth writes and reads data from a file using DataInput/OutputStreams.

Uploaded by

html backup
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)
26 views5 pages

Java Program Examples for Beginners

The document contains 5 code examples that demonstrate different Java programming concepts. The first example checks if a number is prime or not. The second creates a Book class and calls its get method. The third throws a custom exception. The fourth draws shapes using graphics. The fifth writes and reads data from a file using DataInput/OutputStreams.

Uploaded by

html backup
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

import java.util.

Scanner;

class P7_1
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int num;

System.out.println("enter number : ");


num=sc.nextInt();

if(num%2==0 || num%3==0 || num%5==0 || num%7==0 || num%11==0)


{
if(num==2 || num==3 || num==5 || num==7 || num==11)
{
System.out.print("prime nuumber : "+num);
}
else
{
System.out.print("not prime nuumber : "+num);
}
}
else
{
System.out.print("prime nuumber : "+num);
}

}
}

or
import java.util.*;

public class P7_1 {

public static void main(String[] args) {

Scanner scan = new Scanner(System.in);

System.out.println("Enter No. : ");


int num = scan.nextInt();

boolean flag = false;

for (int i = 2; i <= num / 2; i++) {

if (num % i == 0) {
flag = true;
break;
}
}

if (!flag)
System.out.println(num + " is a prime number.");
else
System.out.println(num + " is not a prime number.");
}
}

2)

package Library;

public class Book


{
public void get()
{
System.out.println("java");
System.out.println("sen");
System.out.println("dcc");
System.out.println("gad");
}
}

import Library.*;

class P7_2
{
public static void main(String args[])
{
Book b=new Book();
b.get();
}
}
3)

import java.util.Scanner;

class InvalidEmployeeId extends Exception


{
InvalidEmployeeId(String str)
{
super(str);
}
}

class P7_3
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int e_id;

try
{
System.out.println("enter Enter employee id : ");
e_id=sc.nextInt();

if(e_id<0)
{
throw new InvalidEmployeeId("enter valid employee id ");
}
System.out.println("valid employee id");
}
catch(InvalidEmployeeId e)
{
System.out.println(e);
}
}
}
4)
import java.applet.Applet;
import java.awt.*;

public class P7_4 extends Applet


{
public void paint(Graphics g)
{
g.drawRect(70,30,60,60);
g.fillOval(85,45,30,30);
}
}

/*
<applet code="P7_4" width=500 height=500>
</applet>
*/

5)
import java.io.*;

public class P8_5{

public static void main(String[] args) throws IOException{

FileOutputStream file = new FileOutputStream("xyz.txt");

DataOutputStream data = new DataOutputStream(file);

data.writeInt(69);
data.writeDouble(34.35);
data.writeChar('a');
data.writeBoolean(false);
data.flush();
data.close();

FileInputStream file1 = new FileInputStream("xyz.txt");

DataInputStream in = new DataInputStream(file1);

System.out.println(in.readInt());
System.out.println(in.readDouble());
System.out.println(in.readChar());
System.out.println(in.readBoolean());

in.close();
}
}

You might also like