Tuesday, 29 September 2020

Create magic box for any odd number from Java

  • Start your Eclipse IDE
  • Type the following code in a class named MagicSquare:

import java.util.Scanner;


public class MagicSquare {


public static void main(String[] args) {

System.out.println("Welcome to MagicBox");

System.out.println("---------------------------------------------------------------------------");

System.out.println("Enter any odd number for which you want to create a magic box.(Greater than 1");

Scanner scan=new Scanner(System.in);

int n=scan.nextInt();

if (n%2==0) {System.out.println("You didn't enter an odd number");System.exit(0);}

System.out.println("---------------------------------------------------------------------------");

int ct=1;

int a[][]=new int[n][n];

int x=(n+1)/2;

int y=(n-1)/2;

for (int i=0;i<n;i++) {

for (int j=0;j<n;j++) {

a[x][y]=ct;

ct++;x++;y--;

if (x==n) {x=0;}

if (y==-1) {y=n-1;}

}

x=(x+1)%n;y++;if (y==n) {y=0;}

}

int num=(int) Math.floor(Math.log10((double)(Math.pow(n, 2)))+1);

for (int i=0;i<n;i++) {

for (int j=0;j<n;j++) {

if (Math.floor(Math.log10((double)a[j][i])+1)>num) {num=a[j][i];}

}

}

String space="";

double spc=0;

for (int i=0;i<n;i++) {

for (int j=0;j<n;j++) {

spc=num-Math.floor(Math.log10((double)a[j][i])+1);

for (int abc=0;abc<spc;abc++) {

space=space+" ";

}

System.out.print(" |"+" "+a[j][i]+space);

space="";

}

System.out.println(" |");

}

}


}



  • Execute the code

Monday, 28 September 2020

Demo of Frequency project in Java


 This is a demo of the project in java about the frequency of a word in a .txt file

Saturday, 26 September 2020

Find frequency of keyword in Java

 This program checks the frequency of a keyword in any .txt file

  • Start your IDE 
  • Type in a class named Frequency :
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;


public class Frequency {

public static void main(String[] args) throws FileNotFoundException {
Scanner scan2=new Scanner(System.in);
String src="";
System.out.println("Enter main file path (Only .txt files approved)");
for (;;){
src=scan2.nextLine();
if (new File(src).exists()==true){break;}
System.out.println("Your file doesn't exist. Please try again.");
}System.out.println("-----------------------------------------------------------------------");
System.out.println("Welcome to Frequency");
System.out.println("-----------------------------------------------------------------------");
for (;;){
Scanner scan=new Scanner(new File(src));
System.out.println("Enter the word that you want to find: (Type $END$ to terminate program.)");
String chr=scan2.next().replace("\"", "");
if (chr.equals("$END$")){System.out.println("Terminating program...");System.out.println("Terminated");break;}
chr=chr.replace("?", "");
chr=chr.replace(";", "");
chr=chr.replace("!", "");
chr=chr.replace(",", "");
chr=chr.replace("'", "");
chr=chr.replace("’", "");
chr=chr.replace(".", "");
chr=chr.replace(":", "");
chr=chr.replace("(", "");
chr=chr.replace(")", "");
chr=chr.replace("]", "");
chr=chr.replace("[", "");
chr=chr.replace("{", "");
chr=chr.replace("}", "");
chr=chr.replace("-", "");
chr=chr.replace("@", "");
chr=chr.replace("%", "");
chr=chr.replace("*", "");
chr=chr.replace("#", "");
chr=chr.replace("`", "");
chr=chr.replace("~", "");
chr=chr.replace("/", "");
chr=chr.replace("\\", "");
chr=chr.replace(">", "");
chr=chr.replace("<", "");
double frq=0;
double total=0;
String y="";
while (scan.hasNext()){
y=scan.next().replace("\"", "");
y=y.replace("?", "");
y=y.replace(";", "");
y=y.replace("!", "");
y=y.replace(",", "");
y=y.replace("’", "");
y=y.replace("'", "");
y=y.replace(".", "");
y=y.replace(":", "");
y=y.replace("(", "");
y=y.replace(")", "");
y=y.replace("]", "");
y=y.replace("[", "");
y=y.replace("{", "");
y=y.replace("}", "");
y=y.replace("-", "");
y=y.replace("@", "");
y=y.replace("%", "");
y=y.replace("*", "");
y=y.replace("#", "");
y=y.replace("`", "");
y=y.replace("~", "");
y=y.replace("/", "");
y=y.replace("\\", "");
y=y.replace(">", "");
y=y.replace("<", "");
//System.out.println(y); Just to see result
total++;
if (y.equalsIgnoreCase(chr)){frq++;}
}
System.out.println("Word appears "+frq+" times");
System.out.println("The word has an appearance frequency in the text of "+frq/total*100+"%");
System.out.println("-----------------------------------------------------------------------");}

}

}



  • Execute the code

Wednesday, 2 September 2020

Tic-tac-toe in Java

  • This is a functional tic-tac-toe game in Java.
  • All things will be explained when you run the code.
  • Type the following code in your IDE

import java.util.Scanner;



public class tic_tac_toe {


public static void main(String[] args) {

String turn="";

Scanner scan=new Scanner(System.in);

System.out.println("Welcome to tic-tac-toe");

System.out.println("Let's start");

System.out.println("Take a sheet of paper and draw a 3-by-3 grid");

System.out.println("Now mark them like this:");

System.out.println("1 2 3");

System.out.println("4 5 6");

System.out.println("7 8 9");

System.out.println("You can tell me your turn by writing only the cell number(1-9), nothing else.");

System.out.println("Only legal play is allowed. You get error in wrong turns.");

for(;;){

loop:{

System.out.println("\nComputer:X in 7");

System.out.println("Your turn:");

turn=scan.next();

if (turn.equals("1")){

System.out.println("Computer:X in 9");

System.out.println("Your turn:");

turn=scan.next();

if (turn.equals("8")){

System.out.println("Computer:X in 3");

System.out.println("Your turn:");

turn=scan.next();

if (turn.equals("5")){System.out.println("Computer:X in 6");System.out.println("Computer won.");break loop;}

if (turn.equals("6")){System.out.println("Computer:X in 5");System.out.println("Computer won.");break loop;}

if (turn!="5" & turn!="6"){System.out.println("Computer:X in 5");System.out.println("Computer won.");break loop;}

}

if (turn!="8"){System.out.println("Computer:X in 8");System.out.println("Computer won.");break loop;} 

}

if (turn.equals("2")){

System.out.println("Computer:X in 1");

System.out.println("Your turn:");

turn=scan.next();

if (turn.equals("4")){

System.out.println("Computer:X in 9");

System.out.println("Your turn:");

turn=scan.next();

if (turn.equals("5")){System.out.println("Computer:X in 8");System.out.println("Computer won.");break loop;}

if (turn.equals("8")){System.out.println("Computer:X in 5");System.out.println("Computer won.");break loop;}

if (turn!="5" & turn!="8"){System.out.println("Computer:X in 5");System.out.println("Computer won.");break loop;}

}

if (turn!="4"){System.out.println("Computer:X in 4");System.out.println("Computer won.");break loop;} 

}

if (turn.equals("3")){

System.out.println("Computer:X in 1");

System.out.println("Your turn:");

turn=scan.next();

if (turn.equals("4")){

System.out.println("Computer:X in 9");

System.out.println("Your turn:");

turn=scan.next();

if (turn.equals("5")){System.out.println("Computer:X in 8");System.out.println("Computer won.");break loop;}

if (turn.equals("8")){System.out.println("Computer:X in 5");System.out.println("Computer won.");break loop;}

if (turn!="5" & turn!="8"){System.out.println("Computer:X in 5");System.out.println("Computer won.");break loop;}

}

if (turn!="4"){System.out.println("Computer:X in 4");System.out.println("Computer won.");break loop;} 

}

if (turn.equals("4")){

System.out.println("Computer:X in 9");

System.out.println("Your turn:");

turn=scan.next();

if (turn.equals("8")){

System.out.println("Computer:X in 3");

System.out.println("Your turn:");

turn=scan.next();

if (turn.equals("5")){System.out.println("Computer:X in 6");System.out.println("Computer won.");break loop;}

if (turn.equals("6")){System.out.println("Computer:X in 5");System.out.println("Computer won.");break loop;}

if (turn!="5" & turn!="6"){System.out.println("Computer:X in 5");System.out.println("Computer won.");break loop;}

}

if (turn!="8"){System.out.println("Computer:X in 8");System.out.println("Computer won.");break loop;} 

}

if (turn.equals("5")){

System.out.println("Computer:X in 1");

System.out.println("Your turn:");

turn=scan.next();

if (turn.equals("4")){

System.out.println("Computer:X in 6");

System.out.println("Your turn:");

turn=scan.next();

if (turn.equals("3")){System.out.println("Computer:X in 9");

System.out.println("Your turn:");

turn=scan.next();

if (turn.equals("2")){System.out.println("Computer:X in 8");System.out.println("Computer won.");break loop;}

if (turn.equals("8")){System.out.println("Computer:X in 2");System.out.println("Match draw.");break loop;}

}

if (turn.equals("9")){System.out.println("Computer:X in 3");

System.out.println("Your turn:");

turn=scan.next();

if (turn.equals("2")){System.out.println("Computer:X in 8");System.out.println("Match draw.");break loop;}

if (turn.equals("8")){System.out.println("Computer:X in 2");System.out.println("Computer won.");break loop;}

}

if (turn.equals("2")){System.out.println("Computer:X in 8");

System.out.println("Your turn:");

turn=scan.next();

if (turn.equals("3")){System.out.println("Computer:X in 9");System.out.println("Computer won.");break loop;}

if (turn.equals("9")){System.out.println("Computer:X in 3");System.out.println("Match draw.");break loop;}

}

if (turn.equals("8")){System.out.println("Computer:X in 2");

System.out.println("Your turn:");

turn=scan.next();

if (turn.equals("3")){System.out.println("Computer:X in 9");System.out.println("Match draw.");break loop;}

if (turn.equals("9")){System.out.println("Computer:X in 3");System.out.println("Computer won.");break loop;}

}

}

if (turn!="4"){System.out.println("Computer:X in 4");System.out.println("Computer won.");break loop;} 

}

if (turn.equals("6")){

System.out.println("Computer:X in 1");

System.out.println("Your turn:");

turn=scan.next();

if (turn.equals("4")){

System.out.println("Computer:X in 5");

System.out.println("Your turn:");

turn=scan.next();

if (turn.equals("3")){System.out.println("Computer:X in 9");System.out.println("Computer won.");break loop;}

if (turn.equals("9")){System.out.println("Computer:X in 3");System.out.println("Computer won.");break loop;}

if (turn!="3" & turn!="9"){System.out.println("Computer:X in 3");System.out.println("Computer won.");break loop;}

}

if (turn!="4"){System.out.println("Computer:X in 4");System.out.println("Computer won.");break loop;} 

}

if (turn.equals("8")){

System.out.println("Computer:X in 1");

System.out.println("Your turn:");

turn=scan.next();

if (turn.equals("4")){

System.out.println("Computer:X in 3");

System.out.println("Your turn:");

turn=scan.next();

if (turn.equals("5")){System.out.println("Computer:X in 2");System.out.println("Computer won.");break loop;}

if (turn.equals("2")){System.out.println("Computer:X in 5");System.out.println("Computer won.");break loop;}

if (turn!="5" & turn!="2"){System.out.println("Computer:X in 5");System.out.println("Computer won.");break loop;}

}

if (turn!="4"){System.out.println("Computer:X in 4");System.out.println("Computer won.");break loop;} 

}

if (turn.equals("9")){

System.out.println("Computer:X in 1");

System.out.println("Your turn:");

turn=scan.next();

if (turn.equals("4")){

System.out.println("Computer:X in 3");

System.out.println("Your turn:");

turn=scan.next();

if (turn.equals("5")){System.out.println("Computer:X in 2");System.out.println("Computer won.");break loop;}

if (turn.equals("2")){System.out.println("Computer:X in 5");System.out.println("Computer won.");break loop;}

if (turn!="5" & turn!="2"){System.out.println("Computer:X in 5");System.out.println("Computer won.");break loop;}

}

if (turn!="4"){System.out.println("Computer:X in 4");System.out.println("Computer won.");break loop;} 

}

}System.out.println("\nNew Game");

}

 


}


}


  • Now execute the code.

Algorithmic Pixel Art: The Beauty of Aliasing

Discussed  here More images in  Google drive Some outputs: Forward to anyone who says aliasing is ugly.