0% found this document useful (0 votes)
71 views

Pyramid: Class Public Static Void For Int For Int If Else

The document contains code snippets for several Java programs: 1) A pyramid pattern program that prints dollar signs in a pyramid shape 2) A prime number checker that determines if a given number is prime 3) Examples demonstrating the public, protected, and private access modifiers

Uploaded by

abhinavsaurabh
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)
71 views

Pyramid: Class Public Static Void For Int For Int If Else

The document contains code snippets for several Java programs: 1) A pyramid pattern program that prints dollar signs in a pyramid shape 2) A prime number checker that determines if a given number is prime 3) Examples demonstrating the public, protected, and private access modifiers

Uploaded by

abhinavsaurabh
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
You are on page 1/ 6

1.

Pyramid
2. class p1 {
3.
public static void main(String agrs[]) {
4.
System.out.println("The Pattern is");
5.
for (int i = 0; i < 5; i++) {
6.
for (int j = 0; j < 5; j++) {
7.
if (j <= i) {
8.
System.out.print(" $");
9.
} else {
10.
System.out.print("
");
11.
}
12.
}
13.
System.out.println();
14.
}
15.
}
16. }

2. Prime No. or NOT


class PrimeNo{
public static void main(String args[]){
int num = Integer.parseInt(args[0]);
int flag=0;
for(int i=2;i<num;i++){
if(num%i==0)
{
System.out.println(num+" is not a Prime
Number");
flag = 1;
break;
}
}
if(flag==0)
System.out.println(num+" is a Prime Number");
}
}

3. Public Protected Private


4. Fibopackage com.java2novice.algos;

public class MyFibonacci {

public static void main(String a[]){

int febCount = 15;


int[] feb = new int[febCount];
feb[0] = 0;

feb[1] = 1;
for(int i=2; i < febCount; i++){
feb[i] = feb[i-1] + feb[i-2];
}

for(int i=0; i< febCount; i++){


System.out.print(feb[i] + " ");
}
}
}
5.Abstract Class
Public class shape{
Public abstract void area();
}
Public class square extends shape{
Public void area(){ar=l*l;}
}
Public class rect extends shape{
Public void area(){ar = l*b;}
}
Public class tri extends shape{
Public void area(){ar=.;}
}
Public class circle extend shape{
Public void area() { ar = ..;}
}

6. Number Palindrome

import java.util.Scanner;

class Palindrome{
public static void main(String args[]){
System.out.print("Enter Number: ");
Scanner read = new Scanner(System.in);
int num = read.nextInt();
int n = num;
//reversing number
int rev=0,rmd;
while(num > 0)
{
rmd = num % 10;
rev = rev * 10 + rmd;
num = num / 10;
}
if(rev == n)
System.out.println(n+" is a Palindrome Number!");
else
System.out.println(n+" is not a Palindrome Number!");
}
}

7. Title case
Upper case
String s1 = t1.getText();
String s2 = s1.toUpperCase();
T2.setText(s2);

Lower Case
String s1=t1.getText();
String s2=s1.toLowerCase();
T2.setText(s2);

3. pubic

Package abc;
Public class AccessDemo
{
Public void test()
{
System.out.println(Example of public class);
}
}

Protected
Class AccessDemo
{
Protected int x = 34;
{
System.out.println(The variables is+x);
}
Class childAccessextendAccessDemo
{}
Public class Access Example.
{
Public static void main(String args[]){
childAccess ca = new childAccess();
ca.showDemo();
ca.X=45;

Private
Class AccessDemo
{
Private int x = 56;
Public void showDemo()

{
System.out.println(the Variable is + x);
}
Private void test Demo
{
System.out.println(It cant be accessed in another class);
}}
Public class Access Example
{
Public static void main(String args[]){
AccessDemo Ad = new AccessDemo();
Ad.testDemo();
Ad.X=5;
Ad.showDemo();
Ad.showDemo();
}
}

10. String Palindrome


String Buffer S1 = new StringBuffer(t1.getText());
String Buffer S2;
S2=S1.reverse();
T2.setText(S2+);

9. Submit Button Code


String s1="",s2="",s3="",s4="",s5="",s6="";
s1=t1.getText();
s2=t2.getText();
s3=t3.getText();
if(r1.isSelected())

s4="M";
else
s4="F";
s5=c1.getSelectedItem().toString();
s6=t4.getText();
int i1=Integer.parseInt(s1);
int i2=Integer.parseInt(s3);
try{
Class.forName("java.sql.Driver");
Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/abhinav","root","ssgs");
Statement st=con.createStatement();
String q="insert into student values("+i1+",'"+s2+"',"+i2+",'"+s4+"','"+s5+"','"+s6+"');";
st.executeUpdate(q);

}
catch(Exception e){System.out.println(e);}

You might also like