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

Java Long

Uploaded by

gamdias321
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
22 views

Java Long

Uploaded by

gamdias321
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 43
2. Write a Java program to display all the numbers between -300 to -1 which when divided by 17 gives 7 as remainder and also displays sum of those numbers. i 2 3 4 5 6 7 8 9 10 " 12 13} public class Sun { public static void main(Stringl] args) { int i, For (i=-300;i8]t;-1;i++) { 44 (4817==-7){ system.out.print(i+ " "): S = sti; + system.out.printIn¢"The sum = “+s): 4, Write a program that displays all the read-only files of a given folder. 1 import java.io.*; 2 public class FileList { 3 public static void main(String[] args) { 4 File dir = new File("e:/java"); 5 if(dir.isDirectory()){ 6 String{] s = dir.list(); 7 for(int i=0;i<s.length:i++){ 8 File f = new File(dir+"/"+s[i]); 9 if(f.canRead()) 10 System.out.println(s(i]); 12} 13 else 14 System.out.println("Its is not directory"); 15 } } 17, 7** * * @author bim study notes */ Q.no 11 Write a Java Program to display all the even numbers from 1 to 500 1 public class tuisait 2 public static void main(stringt} args) ¢ 3 int i; 4 For(izt;i61 5 afcinaes 6 Systen.out.printin(i); 7 8 > 3) wo} 11_s## * + eauthor bim study notes */ 12, Make a thread using Runnable interface to display numbers from 1 to 20, each number should be displayed in the interval of 2 seconds. ‘Claes Threadiun inplenents Runnablet public void run(( try for(int i=t;i82t;=20;i6+94 System.out.print¢ie" "); ‘Thread. sleep(2000); > }eatch(Interruptedéxception e)¢ Systom.out.printIn(*Thread Interrupted"); 10) n> eavauvaun- + 13. 7#* + * @author bim study notes */ 14 public class EventusThreaddeno ¢ 15 public static void main(stringt] args) ¢ 16 ThreadNum n = new Thresdiiun(); 17 Thread & = new Thread(n): 18 tastartcns 9) 20) 13, Write a program that reads line of text from keyboard and write to file. Also read the content of the same file and display on monitor, 1 import java.io. 2 class ReadingFromkeyBoard { public static void main(String args{]) throws l0Exception { String str; BufferedReader br = new BufferedReader(nen InputStreamReader (System.in)): Filewriter fw=null; System.out.println(“Enter line of text and ‘stop’ at the end"); try fw = new FileWriter("d://janak//Helllo.txt™); yee * * @author bin study notes */ dot str = br.readtine(): if(str.compareTo ("stop")==0)break; str = stre"\r\n"; fo.write(str); dwhile(str.compareTo("stop™)!=0); }eatch(IOException e){ System.out.printIn(1/0 Error occured”); } finallyt if(fwt=null) fw.close(); , \Q.no 14 7 class Rectanget 2 int length, breadth, area: 3 public void computeArea(int 1, int b){ 4 5 6 3 7} 8 public void displayArea(){ 9 System.out.printIn(“The area is: * +area); 10 } ny 12 public class tu1sq14 extends Rectang{ 3 public static void main(Stringl] args) { 14 Rectang obj1= new Rectang(); 15 obj1.computeArea(10, 20); 16 Rectang obj2=new Rectang(); 7 obj2.computeArea(34, 20); 18 if(obj1.area>obj2.area){ 19 obj1.displayArea(); 20 } 2 else 22 { 23 0bj2.displayArea(); 24 } 25 } 26 + 27_{** * * @author bim study notes */ - Make an interface named num with two functions int add(int x, int y) and Int diff(int x, Int y) then, ‘ke a class that implements that interface num interface nun{ int addcint x,int y); int diffint x,int y); class calc implements nun ¢ int sun; int sub; Override public int add(int x,int y)¢ sum=xsy; system. return su; it printin¢“The sum is ~+sun) » @override public int diff(int x,int y){ Sub=x- > i 5 : 5 , System.out.printIn(*The difference is *+sub): 5 return sub; yy 3 public static void main(stringt) args) ¢ 1 calc obj=nen calc(); 5 ' 5 obj.addc2, 3 obj-diff(30, 20); y > Group C