Java Shape Drawing Program
VerifiedAdded on  2019/10/18
|10
|1283
|284
Practical Assignment
AI Summary
This Java program allows users to draw various shapes like circles, Xs, and boxes, with options to fill them. The program takes user input for shape dimensions and fill preferences. It then generates the shape using asterisks (*) and spaces, displaying it on the console and writing the output to a file named "shape.txt". The code includes functions for drawing each shape, handling user input validation, and writing the output to a file. The program uses a menu-driven approach, allowing users to select the shape they want to draw or exit the program. Error handling is implemented to manage invalid user inputs, such as negative radii or sizes. The program demonstrates fundamental programming concepts such as loops, conditional statements, and file I/O in Java.

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package main;
import java.util.Scanner;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
/**
*
* @author pkum80
*/
public class Main {
public static void main(String[] args) {
int choice =0;
Scanner sc=new Scanner(System.in);
boolean fill = false;
do
{
System.out.println("1. Draw Circle");
System.out.println("2. Draw X");
System.out.println("3. Draw Box");
System.out.println("4. Draw Box with X");
System.out.println("5. Exit");
System.out.print("Please enter your choice ?");
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package main;
import java.util.Scanner;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
/**
*
* @author pkum80
*/
public class Main {
public static void main(String[] args) {
int choice =0;
Scanner sc=new Scanner(System.in);
boolean fill = false;
do
{
System.out.println("1. Draw Circle");
System.out.println("2. Draw X");
System.out.println("3. Draw Box");
System.out.println("4. Draw Box with X");
System.out.println("5. Exit");
System.out.print("Please enter your choice ?");
Paraphrase This Document
Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser

choice = sc.nextInt();
switch(choice)
{
case 1:
double r =0.0;
while(true)
{
System.out.print("Please enter radius of circle ?");
r = sc.nextDouble();
if(r>0)
{
break;
}
else
{
System.out.println("Radius cannot be less than 0");
}
}
sc.nextLine();
System.out.print("Do you want to fill the object ?");
if(sc.nextLine().equalsIgnoreCase("Y"))
fill = true;
else
fill = false;
drawCircle(r,fill);
break;
case 2:
int x_size =0;
switch(choice)
{
case 1:
double r =0.0;
while(true)
{
System.out.print("Please enter radius of circle ?");
r = sc.nextDouble();
if(r>0)
{
break;
}
else
{
System.out.println("Radius cannot be less than 0");
}
}
sc.nextLine();
System.out.print("Do you want to fill the object ?");
if(sc.nextLine().equalsIgnoreCase("Y"))
fill = true;
else
fill = false;
drawCircle(r,fill);
break;
case 2:
int x_size =0;

while(true)
{
System.out.print("Please enter Size of X ?");
x_size = sc.nextInt();
if(x_size>2)
{
break;
}
else
{
System.out.println("Size cannot be less than 3");
}
}
printX(x_size);
break;
case 3:
int box_size =0;
while(true)
{
System.out.print("Please enter Size of Box ?");
box_size = sc.nextInt();
if(box_size>1)
{
break;
}
else
{
System.out.println("Size cannot be less than 2");
{
System.out.print("Please enter Size of X ?");
x_size = sc.nextInt();
if(x_size>2)
{
break;
}
else
{
System.out.println("Size cannot be less than 3");
}
}
printX(x_size);
break;
case 3:
int box_size =0;
while(true)
{
System.out.print("Please enter Size of Box ?");
box_size = sc.nextInt();
if(box_size>1)
{
break;
}
else
{
System.out.println("Size cannot be less than 2");
⊘ This is a preview!⊘
Do you want full access?
Subscribe today to unlock all pages.

Trusted by 1+ million students worldwide

}
}
sc.nextLine();
System.out.print("Do you want to fill the object ?");
if(sc.nextLine().equalsIgnoreCase("Y"))
fill = true;
else
fill = false;
drawSquare(box_size,fill);
break;
case 4:
int box_with_x_size =0;
while(true)
{
System.out.print("Please enter Size of Box ?");
box_with_x_size = sc.nextInt();
if(box_with_x_size>4)
{
break;
}
else
{
System.out.println("Size cannot be less than 5");
}
}
drawSquarewithX(box_with_x_size);
}
sc.nextLine();
System.out.print("Do you want to fill the object ?");
if(sc.nextLine().equalsIgnoreCase("Y"))
fill = true;
else
fill = false;
drawSquare(box_size,fill);
break;
case 4:
int box_with_x_size =0;
while(true)
{
System.out.print("Please enter Size of Box ?");
box_with_x_size = sc.nextInt();
if(box_with_x_size>4)
{
break;
}
else
{
System.out.println("Size cannot be less than 5");
}
}
drawSquarewithX(box_with_x_size);
Paraphrase This Document
Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser

break;
case 5:
return;
default:
System.out.println("Invalid choice");
}
}while(choice !=5);
}
public static void printX(int x) {
StringBuffer str = new StringBuffer("");
char[] chars = new char[x];
for (int i = 0; i < x; i++) {
chars[i] = '*';
chars[x - 1 - i] = '*';
for (int j = 0; j < x; j++) {
if (j == i || j == (x - 1 - i)) {
continue;
}
chars[j] = ' ';
}
System.out.println(new String(chars));
str.append(new String(chars));
str.append("\n");
}
writeToFile(str.toString());
}
case 5:
return;
default:
System.out.println("Invalid choice");
}
}while(choice !=5);
}
public static void printX(int x) {
StringBuffer str = new StringBuffer("");
char[] chars = new char[x];
for (int i = 0; i < x; i++) {
chars[i] = '*';
chars[x - 1 - i] = '*';
for (int j = 0; j < x; j++) {
if (j == i || j == (x - 1 - i)) {
continue;
}
chars[j] = ' ';
}
System.out.println(new String(chars));
str.append(new String(chars));
str.append("\n");
}
writeToFile(str.toString());
}

public static void drawSquare(int s,boolean fill)
{
StringBuffer str = new StringBuffer("");
for(int j=1; j<=s; j++)
{
for(int i=1; i<=s; i++)
{
if(j ==1 || j==s || i==1 || i==s)
{
System.out.print("* ");
str.append("* ");
}
else
{
if(fill)
{
System.out.print("* ");
str.append("* ");
}
else
{
System.out.print(" ");
str.append(" ");
}
}
}
str.append("\n");
{
StringBuffer str = new StringBuffer("");
for(int j=1; j<=s; j++)
{
for(int i=1; i<=s; i++)
{
if(j ==1 || j==s || i==1 || i==s)
{
System.out.print("* ");
str.append("* ");
}
else
{
if(fill)
{
System.out.print("* ");
str.append("* ");
}
else
{
System.out.print(" ");
str.append(" ");
}
}
}
str.append("\n");
⊘ This is a preview!⊘
Do you want full access?
Subscribe today to unlock all pages.

Trusted by 1+ million students worldwide

System.out.println();
}
writeToFile(str.toString());
}
public static void drawSquarewithX(int s)
{
StringBuffer str = new StringBuffer("");
for(int j=1; j<=s; j++)
{
for(int i=1; i<=s; i++)
{
if(j ==1 || j==s || i==1 || i==s)
{
System.out.print("* ");
str.append("* ");
}
else
{
if(i!= 1 && i==j)
{
System.out.print("* ");
str.append("* ");
}
else if(i-s!=0 && s-i == j-1)
{
System.out.print("* ");
str.append("* ");
}
}
writeToFile(str.toString());
}
public static void drawSquarewithX(int s)
{
StringBuffer str = new StringBuffer("");
for(int j=1; j<=s; j++)
{
for(int i=1; i<=s; i++)
{
if(j ==1 || j==s || i==1 || i==s)
{
System.out.print("* ");
str.append("* ");
}
else
{
if(i!= 1 && i==j)
{
System.out.print("* ");
str.append("* ");
}
else if(i-s!=0 && s-i == j-1)
{
System.out.print("* ");
str.append("* ");
}
Paraphrase This Document
Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser

else
{
System.out.print(" ");
str.append(" ");
}
}
}
str.append("\n");
System.out.println();
}
writeToFile(str.toString());
}
public static void drawCircle(double r, boolean fill)
{
StringBuffer str = new StringBuffer("");
double r_in = r - 0.4;
double r_out = r + 0.4;
for ( double y = r; y >= -r; --y )
{
for ( double x = -r; x < r_out; x += 0.5 )
{
double value = x * x + y * y;
if ( value >= r_in * r_in && value <= r_out * r_out )
{
System.out.print('*');
str.append("*");
{
System.out.print(" ");
str.append(" ");
}
}
}
str.append("\n");
System.out.println();
}
writeToFile(str.toString());
}
public static void drawCircle(double r, boolean fill)
{
StringBuffer str = new StringBuffer("");
double r_in = r - 0.4;
double r_out = r + 0.4;
for ( double y = r; y >= -r; --y )
{
for ( double x = -r; x < r_out; x += 0.5 )
{
double value = x * x + y * y;
if ( value >= r_in * r_in && value <= r_out * r_out )
{
System.out.print('*');
str.append("*");

}
else
{
if(fill)
{
if(value <=r_out * r_out )
{
System.out.print('*');
str.append("*");
}
else
{
System.out.print(' ');
str.append(" ");
}
}
else
{
System.out.print(' ');
str.append(" ");
}
}
}
System.out.print("\n");
str.append("\n");
}
writeToFile(str.toString());
}
else
{
if(fill)
{
if(value <=r_out * r_out )
{
System.out.print('*');
str.append("*");
}
else
{
System.out.print(' ');
str.append(" ");
}
}
else
{
System.out.print(' ');
str.append(" ");
}
}
}
System.out.print("\n");
str.append("\n");
}
writeToFile(str.toString());
}
⊘ This is a preview!⊘
Do you want full access?
Subscribe today to unlock all pages.

Trusted by 1+ million students worldwide

public static void writeToFile(String s)
{
//System.out.println(s);
try{
// create new file
//String content = "This is the content to write into create file";
String path="shape.txt";
File file = new File(path);
// if file doesnt exists, then create it
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
// write in file
bw.write(s);
// close connection
bw.close();
}catch(Exception e){
System.out.println(e);
}
}
}
{
//System.out.println(s);
try{
// create new file
//String content = "This is the content to write into create file";
String path="shape.txt";
File file = new File(path);
// if file doesnt exists, then create it
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
// write in file
bw.write(s);
// close connection
bw.close();
}catch(Exception e){
System.out.println(e);
}
}
}
1 out of 10
Related Documents

Your All-in-One AI-Powered Toolkit for Academic Success.
 +13062052269
info@desklib.com
Available 24*7 on WhatsApp / Email
Unlock your academic potential
Copyright © 2020–2025 A2Z Services. All Rights Reserved. Developed and managed by ZUCOL.