Recent Post

Sunday, October 26, 2014

(Game: display a checkerboard) Write a program that displays a checkerboard
in which each white and black cell is a  Rectangle with a fill color black or
white, as shown in figure.

Solution:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

public class Checkboard extends Application {

@Override
public void start(Stage primaryStage) {
Pane pane = new Pane();
int i = 0;
int j = 0;

for(i = 0; i < 8; i++) {
for(j = 0; j < 8; j++) {
Rectangle rec = new Rectangle(20 * j, 20 * i, 20, 20);
if(i + j % 2 == 1) {
rec.setFill(Color.BLACK);
} else {
rec.setFill(Color.WHITE);
}

pane.getChildren().add(rec);
}
}

Scene scene = new Scene(pane, 160, 160);
primaryStage.setTitle("Checkboard");
primaryStage.setScene(scene);
primaryStage.show();
}

public static void main(String[] args) {
launch(args);
}
}
5

(Color and font) Write a program that displays five texts vertically, as shown in figure below. Set a random color and opacity for each text and set the font of each text to Times Roman, bold, italic, and 22 pixels.


Solution:


import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class TextJava extends Application {

@Override
public void start(Stage primaryStage) {
Pane pane = new Pane();
Font font = Font.font("Times New Roman", FontWeight.BOLD, FontPosture.ITALIC, 22);
Text[] texts = new Text[5];

for(int i = 0; i < texts.length; i++) {
texts[i] = new Text(20 * i, 50, "JAVA");
texts[i].setFont(font);
texts[i].setFill(new Color(Math.random(), Math.random(), Math.random(), Math.random()));
texts[i].setRotate(90);
pane.getChildren().add(texts[i]);
}

Scene scene = new Scene(pane, 100, 100);
primaryStage.setTitle("JAVA with color");
primaryStage.setScene(scene);
primaryStage.show();

}

public static void main(String[] args) {
launch(args);
}
}

0

Saturday, October 25, 2014

(Tic-tac-toe board) Write a program that displays a tic-tac-toe board, as shown
in figure below. A cell may be X, O, or empty. What to display at each cell is
randomly decided.


Solution:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;

public class TicTacToe extends Application {

@Override
public void start(Stage primaryStage) {
GridPane pane = new GridPane();
Image img1 = new Image("image/o.gif");
Image img2 = new Image("image/x.gif");
ImageView imgView1 = new ImageView(img1);
ImageView imgView2 = new ImageView(img2);
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; j++) {
int random = (int)(Math.random() * 2);
if(random == 0) {
pane.add(imgView1, j, i);
} else {
pane.add(imgView2, j, i);
}
}
}
Scene scene = new Scene(pane);
primaryStage.setTitle("Tic-Tac-Toe Board");
primaryStage.setScene(scene);
primaryStage.show();
}

public static void main(String[] args) {
Application.launch(args);
}
}

1

(Display images) Write a program that displays four images in a grid pane, as
shown in figure below:


Solution:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;

public class Flags extends Application {

@Override
public void start(Stage primaryStage) {
GridPane pane = new GridPane();
Image img1 = new Image("image/ca.gif");
Image img2 = new Image("image/china.gif");
Image img3 = new Image("image/us.gif");
Image img4 = new Image("image/uk.gif");
ImageView imgView1 = new ImageView(img1);
ImageView imgView2 = new ImageView(img2);
ImageView imgView3 = new ImageView(img3);
ImageView imgView4 = new ImageView(img4);
pane.add(imgView1, 1, 0);
pane.add(imgView2, 0, 1);
pane.add(imgView3, 1, 1);
pane.add(imgView4, 0, 0);
Scene scene = new Scene(pane);
primaryStage.setTitle("Nations Flag");
primaryStage.setScene(scene);
primaryStage.show();
}

public static void main(String[] args) {
launch(args);
}
}

0

Saturday, June 21, 2014

Write a program that receives a character and displays its Unicode.

Solution:

public static void main(String[] args) {
Scanner s = new Scanner(System.in);
String str;
char ch;

System.out.print("Enter a character:");
str = s.nextLine();

ch = str.charAt(0);

System.out.print("The Unicode for the character " + ch + " is " + (int)ch + ".");
}
1

Write a program that receives an ASCII code (an integer between  0 and  127 ) and displays its character.

Solution:

public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int ch;

System.out.print("Enter an ASCII code:");
ch = s.nextInt();

if(ch < 0 || ch > 127) {
System.out.print("Invalid data. Number must be between 0 and 127.");
} else {
System.out.print("The character for ASCII code " + ch + " is " + (char)ch);
}
}
0

A regular polygon is an n-sided polygon in which all sides are of the same length and all angles have the same degree (i.e., the polygon is both equilateral and equiangular). The formula for computing the area of a regular polygon is:

Here, s is the length of a side. Write a program that prompts the user to enter the number of sides and their length of a regular polygon and displays its area. 

Solution:

public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int n;
double side, area;

System.out.print("Enter the number of sides:");
n = s.nextInt();

System.out.print("Enter the side:");
side = s.nextDouble();

area = (n * Math.pow(side, 2)) / (4 * Math.tan(Math.toRadians(Math.PI / n)));

System.out.print("The area of the polygon is " + area);
}
0

Author