EXPERIMENT 21 and 22: X. Program Code

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 18

EXPERIMENT 21 and 22

X. Program Code
XIII. Exercise

(1)
(3)
import java.awt.*;
import java.applet.*;
public class Shapes extends Applet
{
public void paint(Graphics g)
{
/*Cylinder*/
g.drawString("(a).Cylinder",10,110);
g.drawOval(10,10,50,10);
g.drawOval(10,80,50,10);
g.drawLine(10,15,10,85);
g.drawLine(60,15,60,85);
/*Cube*/
g.drawString("(b).Cube",95,110);
g.drawRect(80,10,50,50);
g.drawRect(95,25,50,50);
g.drawLine(80,10,95,25);
g.drawLine(130,10,145,25);
g.drawLine(80,60,95,75);
g.drawLine(130,60,145,75);
/*Squar Inside A Circle*/
g.drawString("(c).Squar Inside A Circle",150,110);
g.drawOval(180,10,80,80);
g.drawRect(192,22,55,55);
/*Circle Inside a Squar*/
g.drawString("(d).Circle Inside a Squar",290,110);
g.drawRect(290,10,80,80);
g.drawOval(290,10,80,80);
/*Polygon*/
g.drawString("(e).Polygon",90,250);
int a[]={200,400,200,400};
int b[]={200,500,500,200};
g.drawPolygon(a,b,4);
}
}/*<applet code="Shapes.class" height=500 width=500></applet>*/
import java.io.*;
import java.util.*;
class ReadWrite
{
public static void main(String[] args) throws FileNotFoundException,IOException
{
String s,s1;
FileOutputStream fo =new FileOutputStream("temp.txt"); Scanner
sc=new Scanner(System.in); System.out.println("enter the data to be
written to file:"); s=sc.nextLine();
byte b[]=s.getBytes();
fo.write(b);
System.out.println("write operation in file performed successfully");
FileInputStream fin =new FileInputStream("temp.txt"); int x=0;
while((x=fin.read())!=-1)
System.out.print((char)x);
System.out.println("\nread operation in file performed successfully");
}
}

XIII Exercise

import java.io.*;
class filecopy1 {
public static void main(String args[]) throws IOException {
FileReader fr=new FileReader("filecopy1.java");
FileWriter fo=new FileWriter("filecopy2.java");
int ch;
try {
while((ch=fr.read())!= -1) {
fo.write(ch);
}
System.out.println("file copied successfully");
fr.close();
fo.close();
}
Finally {
if(fr!=null)
fr.close();
if(fo!=null)
fo.close();
}}}
import java.io.*;
public class WriteBytes {
public static void main (String[] args) {
byte cities[] = {'M', 'e', 'l', 'b', 'o', 'u', 'r', 'n', 'e', '\n', 'S', 'y', 'd', 'n', 'e', 'y', '\n' };
FileOutputStream outFile;
Try {
outFile = new FileOutputStream("City.txt");
outFile.write(cities);
outFile.close();
}
catch(IOException e) {
System.out.println("Exception generated");
}
import java.io.*;
public class FileDisplay
{
public static void main (String[] args)
{
System.out.println("Aastha Bhatt 06");
if(args.length != 1)
{
System.out.println("Error: in sufficient arguments");
System.out.println("Usage - java FileDisplay SourceFile");
System.exit(-1);
}
try {
FileReader srcFile = new FileReader(args[0]);
int ch;
while((ch=srcFile.read()) != -1)
System.out.print((char)ch);
srcFile.close();
}
catch(IOException e)
{
System.out.println(e);
System.exit(-1);
}
}
}

You might also like