Advance Programing in java

profilesandeep@45
JAVA.docx

Assignment

Overview

This project consists of two (2) tasks. The goal is to explore how to design GUI applications and examine how to create animation via multi-threading. Glance at “What to Submit” when you start working on a task so that you know what information to provide from each task.

What to Submit

1. One doc file “csci531-project-FirstnameLastname.doc” including the text source code and screen snapshots of the output of all programs. Please replace Firstname and Lastname with your first name and last name. You can copy/paste the text source code from Eclipse or other IDEs into the doc file. Hopefully, based on the screen snapshots of the output, you can show that your programs passed tests and were functioning well.

2. Java class files for all programs. In well-defined programs, proper comments are required. For Programs without comments, they will be deducted greatly in grade.

· For task 1, there are 1 java file - Task1aFirstnameLastname.java and 1 txt file named “users.txt”.

· For task 2, there are 3 files including Task2FirstnameLastname.java, Car.java, and CarComponent.java.

3. Note that if any program or code does not work, you can explain the status of the program or code and then attach your explanation and description in a file “README.txt”.

4. Optional. Anything you want to attract the attention of instructor in gradeing.

Task 1 : Write a program with a graphical interface that implements a login window with text fields for the user name and password, as shown in Fig. 1. When the login is successful, hide the login window and open a new window with a welcome message. Follow these rules for validating the password:

· The user name is not case sensitive.

· The password is case sensitive.

· The user has three opportunities to enter valid credentials.

Otherwise, display an error message and terminate the program. When the program starts, read the file users.txt. Each line in that file contains a user name and

Figure 1: Example GUI

Figure 2: Sample run 1

Figure 3: Sample run 2

password, separated by a space. You should make a users.txt file for testing your program. Note that you should have at least 10 users in your program.

The figures below show an example GUI of this program and 2 sample runs with different users with passwords.

Sample Run 1: In this sample run, the user entered “jessica” and the password is “1234” as indicated in the file “users.txt”. A pop-up window shows the welcome message. The result is shown in Fig. 2.

Sample Run 2: In this sample run, the user entered “mark”, who is not a registered user. The result is shown in Fig. 3.

If time allows, you can also add a function “Not enrolled? Sign up now” for 10 bonus points.

Grading Rubric

· 10 points for the class with try/catch/finally block.

· 30 points for the workable functions including login with only three opportunities, welcome window, and error message window.

· 5 points for the screenshots of outputs.

· 5 points for appropriate comments

Task 2 : Write a program that produces the following figures and shows two cars moving across a window based on following program segments. Use a separate thread for each car.

/**

* Task2FirstnameLastname.java

*/ import javax.swing.JFrame;

public class Task2FirstnameLastname

{

public static void main(String[] args)

{

JFrame frame = new JFrame();

final int FRAMEWIDTH = 600; final int FRAMEHEIGHT = 400;

frame.setSize(FRAMEWIDTH, FRAMEHEIGHT); frame.setTitle("Two cars");

frame.setDefaultCloseOperation(JFrame.EXIT ON CLOSE);

CarComponent component = new CarComponent(); frame.add(component);

frame.setVisible(true);

// add statements below

. . .

}

}

/**

* CarComponent.java

*/ import java.awt.Graphics; import java.awt.Graphics2D; import javax.swing.JComponent;

/**

This component draws two car shapes.

*/

public class CarComponent extends JComponent

{

public void paintComponent(Graphics g)

{

Graphics2D g2 = (Graphics2D) g; Car car1 = new Car(0, 0);

int x = getWidth() − 60; int y = getHeight() − 30; Car car2 = new Car(x, y);

car1.draw(g2); car2.draw(g2); }

// add statements and methods below

. . .

}

/**

* Car.java

*/ import java.awt.Graphics2D; import java.awt.Rectangle; import java.awt.geom.Ellipse2D; import java.awt.geom.Line2D; import java.awt.geom.Point2D;

/**

A car shape that can be positioned anywhere on the screen.

*/

public class Car

{

private int xLeft; private int yTop;

/**

Constructs a car with a given top left corner.

@param x the x coordinate of the top left corner

@param y the y coordinate of the top left corner

*/

public Car(int x, int y)

{

xLeft = x; yTop = y;

}

// refer to the provided java file // add statements and methods below

. . .

}

Grading Ruberic

30 points for three runnable classes.

5 points for appropriate comments

5 points for the screenshots of outputs.

Challenges in This Project

1. For bonus points, you are welcome to explore the design of each task. Note: You still have to finish all tasks required by this project.

2. You can also install the plugin Window Builder on Eclipse to facilitate the project development.

3. For car animation, you should have two threads simultaneously and update the position of cars.

4. You can consider connecting to a database for achieving and maintaining user information in a more professional manner.