Announcement:

This is just the beginning of this blog, please don't copy any of my posts.

Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Sunday, 8 September 2013

Teach Yourself Java in 21 Days [eBook][Free Download]

This book pdf package combines the best Java tutorial with an extensive reference section, plus new coverage of advanced Java programming topics--all in a durable, high-quality hardcover binding. The pdf contains the entire book in electronic form, source code for the book's examples, additional Java applets, Sun's Java Development Kit for Windows, Solaris and Macintosh, and a collection of the best third-party Java development tools.

Some Screenshots from the ebook as below:




Sunday, 23 December 2012

[Java] Implement a Program to Perform Various Operations on Text Files

Implement a program to accomplish the following task:
  • Create a text file and store data in it.
  • Count number of lines and words in the file.
  • Copy contents of one file to another file.

import java.io.*;
class Copycon
{
int count1,count,len;
void getdata()
{
try
{
RandomAccessFile ff=new RandomAccessFile ("student.dat","rw");
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
String choice=new String();
String s="";
int countc=count1=len=0;

do{
countc++;
count1++;
System.out.print("Enter the String :");
s=in.readLine();
ff.writeUTF(s);
len=s.length();
s=s.trim();

for(int i=0;i<len;i++)
{
if(s.charAt(i)==' ')
countc++;
}
System.out.print("Insert next line(Y/N):");
choice=in.readLine();
}
while(choice.compareTo("Y")==0||choice.compareTo("y")==0);
System.out.println("Number of Lines:"+count1);
System.out.println("Number of words:"+countc);
}
catch(Exception e)
{   }
}
public static void main(String args[])
{
Copycon c=new Copycon();
c.getdata();
File inFile=new File("input.txt");
File outFile=new File("output.txt");
FileReader ins=null;
FileWriter outs=null;
try
{
ins=new FileReader(inFile);
outs=new FileWriter(outFile);
int ch;
while((ch=ins.read())!=-1);
{
outs.write(ch);
}
}
catch(Exception e)
{
System.out.println(e);
System.out.println(-1);
}
finally
{
try
{
ins.close();
outs.close();
}
catch(IOException e)
{}
}

[Java] program to calculate and display area of Square and Rectangle using function overloading

Define a class Shape having overloading static member function area() to calculate and display area of Square and Rectangle.
class Shape
{
static int l,b,S,R;
static void area(int m)
{                                                                                                  //For Square
l=m;     
}

static void area(int x,int y)
{
l=x;                                                                                            //For Rectangle
b=y;
}

void cal()

{
S=l*l;
R=l*b;
}

void display()
{
System.out.println("Area of Square="+S);                       //For Display
System.out.println("Area of Rectangle="+R);
}

public static void main(String args[])
{
Shape s1=new Shape();                                                       //Object s1 of class Shape is created.
area(10);
area(10,2);                                                                             // area() is called .
s1.cal();
s1.display();
}
}

[Java] Program to calculate area of Circle and display it

Define a class Circle having data members pi and radius. Initialize and display values of data members also calculate area of Circle and display it.


class Circle
{
int radius;
double PI=3.14,area;
Circle(int m)
{
radius=m;
}
void area()
{
area=PI*radius*radius;
}
void display()
{
System.out.println("Radius of circle="+radius);
System.out.println("Area of circle="+area);
}
public static void main(String args[])
{
Circle c=new Circle(10);
c.area();
c.display();
}  }

[Java] Program to calculate total marks and percentage scored by student using methods

Define a class Student with four data members such as name, roll no, sub1, sub2.

  • Define appropriate methods to initialize and display the value of data members.
  • Also calculate total marks and percentage scored by student.

 class Student
{
int roll_no,sub1,sub2,total;
String name;
float per;
Student(int a,String b,int c,int d)
{
roll_no=a;
name=b;
sub1=c;
sub2=d;
}
void cal()
{
total=sub1+sub2;
per=(float)total/200*100;
}
void display()
{
System.out.println("Name of student="+name);
System.out.println("Roll number="+roll_no);
System.out.println("Subject1 marks="+sub1);
System.out.println("Subject2 marks="+sub2);
System.out.println("Total marks="+total);
System.out.println("Percentage="+per);
}
public static void main(String args[])
{
Student s1=new Student(11,"Swati",68,82);
s1.cal();
s1.display();
}
}

Define a class having one 3-digit number ,num as data member. Initialize and display reverse of that number.

Define a class having one 3-digit number , num as data member. Initialize and display reverse of that number.

class Prog2

{
public static void main(String args[])
{
  int n=123;
  System.out.println("number is:"+n);
  while(n>0)
  {
    int rem=n%10;
    System.out.print(rem);
   n=n/10;
}
}
}

[JAVA] Define a class having three numbers as data members. Initialize and display the greatest of the three numbers.

Program to  Define a class having three numbers as data members. Initialize and display the greatest of the three numbers.

class Prog3

{
public static void main(String args[])
{
 int a=20,b=8,c=10;
 if(a>b && a>c)
 {
 System.out.println("The greatest no. is a:"+a);
 }
 else if(b>a && b>c)
 {
 System.out.println("The greatest no. is b:"+b);
 }
 else  if(c>a && c>b)
 {
 System.out.println("The greatest no. is c:"+c);
 }
 else
 {
 System.out.println("all are equal");
 }
}
}

Copyright @ 2013 Pune University Bachelor of Engineering . Designed by Pankaj Gaikar | Love for The Tricks Machine