Write a program which creates a file and then asks user to write into that file
ID: 3552404 • Letter: W
Question
Write a program which creates a file and then asks user to write into that file and save the file. Then the program prints the content of the file. You can use any technique to let the program know when it should read the content of the file.
Write a program that calculates the factors of a positive integer number. The program should prompt user upon entering an invalid entry (the only valid entry is a positive integer).
Write a program that prints a shape similar to the following shape. Maximize use of loops and minimize use of print statements.
*
**
***
****
***
**
*
Write an applet that uses one drawLine statement, one drawRect statement and a few loops to draw a cube.
Explanation / Answer
import java.util.*;
import java.io.*;
public class ReadWrite {
public static void main(String [] args) {
// The name of the file to open.
String fileName = "temp.txt";
// This will reference one line at a time
String line = null;
System.out.println("Do you want to read or write to the file:(r/w)");
Scanner s=new Scanner(System.in);
String str=s.nextLine();
if (str.equals("r"))
{
try {
FileReader fileReader =
new FileReader(fileName);
// Always wrap FileReader in BufferedReader.
BufferedReader bufferedReader =
new BufferedReader(fileReader);
while((line = bufferedReader.readLine()) != null) {
System.out.println(line);
}
// Always close files.
bufferedReader.close();
}
catch(FileNotFoundException ex) {
System.out.println(
"Unable to open file '" +
fileName + "'");
}
catch(IOException ex) {
System.out.println(
"Error reading file '"
+ fileName + "'");
// Or we could just do this:
// ex.printStackTrace();
}
}
else{
try {
// Assume default encoding.
FileWriter fileWriter =
new FileWriter(fileName);
// Always wrap FileWriter in BufferedWriter.
BufferedWriter bufferedWriter =
new BufferedWriter(fileWriter);
// Note that write() does not automatically
// append a newline character.
bufferedWriter.write("Hello there,");
bufferedWriter.write(" here is some text.");
bufferedWriter.newLine();
bufferedWriter.write("We are writing");
bufferedWriter.write(" the text to the file.");
// Always close files.
bufferedWriter.close();
}
catch(IOException ex) {
System.out.println(
"Error writing to file '"
+ fileName + "'");
// Or we could just do this:
// ex.printStackTrace();
}
}
}
}
---------------------------------------------------------
//counting the factors of a positive integer
import java.util.*;
public class CountFactors
{
public static void main(String args[])
{
System.out.println("Program for Counting no of factors");
System.out.println("Enter a no to count factors");
Scanner ss=new Scanner(System.in);
int n = ss.nextInt();
if (n>0)
System.out.println("no of factors "+count_factors(n));
else
System.out.println("Enter a positive integer");
}
public static int count_factors(int N)
{
int result = 0;
final int sqrtN = (int) Math.sqrt(N);
for (int i = 1; i <= sqrtN; i++)
{
if (N % i == 0)
{
// We found 2 factors: i and N/i.
result += 2;
}
}
if (sqrtN * sqrtN == N)
{
// We counted sqrtN twice.
result--;
}
return result;
}
}
-------------------------------
//printing the stars....
import java.util.*;
public class PrintStars
{
public static void main(String args[])
{
System.out.println("Program for displaying pattern of *.");
System.out.println("Enter the maximum no of stars: ");
Scanner ss=new Scanner(System.in);
int n = ss.nextInt();
for (int i = 1; i <= n; i++)
{
for (int j = 0; j < (n - i); j++)
System.out.print(" ");
for (int j = 1; j <= i; j++)
System.out.print("*");
for (int k = 1; k < i; k++)
System.out.print("*");
System.out.println();
}
for (int i = n - 1; i >= 1; i--)
{
for (int j = 0; j < (n - i); j++)
System.out.print(" ");
for (int j = 1; j <= i; j++)
System.out.print("*");
for (int k = 1; k < i; k++)
System.out.print("*");
System.out.println();
}
System.out.println();
}
}
------------------------------
//Program to write applets to draw Cube
//run it by appletViwer rather in browser as some browser may not support applets
import java.awt.*;
import java.applet.*;
/**/
public class Cube extends Applet
{
public void init()
{
setBackground(Color.yellow);
}
public void paint(Graphics g)
{
/*Cube*/
g.drawString("Cube",95,110);
g.setColor(Color.red);
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);
}
}
/*<applet code="Cube" width=400 height=400> </applet>*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.