Question
Requirements One class, one file. Don't create multiple classes in the same .java file Don't use static variables and methods Encapsulation: make sure you protect
Requirements
- One class, one file. Don't create multiple classes in the same .java file
- Don't use static variables and methods
- Encapsulation: make sure you protect your class variables and provide access to them through get and set methods
- all the classes are required to have a constructor that receivesallthe attributes as parameters and update the attributes accordingly
- Follow Horstmann's Java Language Coding GuidelinesLinks to an external site.
- Organized in packages (MVC - Model - View Controller)
Contents
The Netbeans Project
You are required to use this NetBeans starter project.
This assignment focus on using the MVC architecture. The main components are the three packages, Model, View and Controller. From the lesson you know that
View objects are objects that the users can see, the graphical part of an application.
Model objects will have all the data and processing needs of an application.
The Controller objects will usually help and support the communication and data transfer between Model and View objects.
The starter NetBeans project that you will use looks like this:
-
- App.java
- Controller
- Controller.java
- Model
- Model.java
- MailAddress.java
- Student.java
- View
- View.java
App.java Controller Controller.java Model MailAddres\" v:shapes=\"Picture_x0020_73\">
Functionality
The main objective is to make View display the information about 3 students. The students are created in Model. Controller will retrieve the data from Model and pass it to View. View will use \"System,out.println\" statements to display the following result:
NAME = Emily Smith Age = 20, address=107 W College Avenue,State College,PA,16801 11 credits
NAME = Mary Doe Age = 20, address=200 W College Avenue,State College,PA,16801 13 credits
NAME = John Doe Age = 20, address=300 W College Avenue,State College,PA,16801 6 credits
The number of credits are calculated on the fly in Model and might vary in the output of your assignment.
You will need to work on Model, Controller and view adding single statements and methods to achieve the lab objectives.
The classes
- App
- it has themainmethod which is the method that Java looks for and runs to start any application
- it creates 3 objects, one of the Model class, one of the View class and one of the Controller class.
- When it creates Controller, it passes the two other objects as input parameters so that Controller has access to Model and View.
Model model = new Model();
View view = new View();
Controller controller = new Controller(model, view);
- Controller
- will retrieve from Model (using the model object) data about each student to be displayed
- will pass the data to View, which will display it
- the two operations above will work best if Model and View have methods that help with that.
- You are writing these methods
- For instance, controller might look like this:
int n1 = 0;
view.displayData(model.findStudent(n1));
int n2 = 1;
view.displayData(model.findStudent(n2));
int n3 = 2;
view.displayData(model.findStudent(n3));
- Model
- it is already given in the starter project although you will need to add some functionality
- it needs a method that returns data about a specific student in the array (you will write this method or a similar one)
public class Model
{
ArrayList sts = new ArrayList();
public Model()
{
//creates 3 students
MailAddress addr1 = new mailAddress(\"107 W College Avenue\", \"State College\", \"PA\", 16801);
Student st1 = new student(\"Emily\", \"Smith\", 20, addr1);
MailAddress addr2 = new mailAddress(\"200 W College Avenue\", \"State College\", \"PA\", 16801);
Student st2 = new student(\"Mary\", \"Doe\", 20, addr2);
MailAddress addr3 = new mailAddress(\"300 W College Avenue\", \"State College\", \"PA\", 16801);
Student st3 = new student(\"John\", \"Doe\", 20, addr3);
//add them to the array of students
sts.add(st1);
sts.add(st2);
sts.add(st3);
}
}
- MailAddress
- it is used by the Student class and doesn't need any updates
- Student
- it is used in Model and doesn't need any updates.
- View
- In this assignment, it is a very simple class (it will get more complex quickly in the next two assignments)
- it just needs to display the text data that it will receive from Controller
- It needs a method to do this
*Default package:
App.java: import Controller.Controller; import Model.Model; import View.View;
class App {
public static void main(String[] args) { Model model = new Model(); View view = new View(); Controller controller = new Controller(model, view); } }
*Controller:
Controller.java:
package Controller;
import Model.Model; import View.View; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Scanner; import javax.swing.JButton;
public class Controller {
Model model; View view; int k = 0;
public Controller(Model model, View view) { this.model = model; this.view = view; }
}
*Model:
MailAddress.java:
package Model;
public class MailAddress {
String streetAddress; String city; String state; int zipcode;
public MailAddress(String inf_streetAddress, String inf_city, String inf_state, int inf_zipcode) { streetAddress = inf_streetAddress; city = inf_city; state = inf_state; zipcode = inf_zipcode; }
@Override public String toString() { return streetAddress + \",\" + city + \",\" + state + \",\" + zipcode; }
}
Model.java:
package Model;
import java.util.ArrayList;
public class Model {
ArrayList sts = new ArrayList();
public Model() { //creates 3 students MailAddress addr1 = new MailAddress(\"107 W College Avenue\", \"State College\", \"PA\", 16801); Student st1 = new Student(\"Emily\", \"Smith\", 20, addr1); MailAddress addr2 = new MailAddress(\"200 W College Avenue\", \"State College\", \"PA\", 16801); Student st2 = new Student(\"Mary\", \"Doe\", 20, addr2); MailAddress addr3 = new MailAddress(\"300 W College Avenue\", \"State College\", \"PA\", 16801); Student st3 = new Student(\"John\", \"Doe\", 20, addr3); //add them to the array of students sts.add(st1); sts.add(st2); sts.add(st3); }
}
Student.java:
package Model;
public class Student { //---------Declaring attributes----
String firstName; String lastName; int age; MailAddress address; int credits; //------------------------------ //----------Constructor------------
public Student(String a, String b, int c, MailAddress d) { firstName = a; lastName = b; age = c; address = d; credits = creditsThisSemester(); }
//---------- METHODS -------- public String getInfo() { return \"NAME = \" + firstName + \" \" + lastName + \" \" + \"Age = \" + age + \", address=\" + address.toString() + \" credits=\" + credits; }
//------------------------------------------------ public int creditsThisSemester() { //calculate randomly a new value for credits in the semester //and updates the attribute credits double dn = Math.random(); credits = (int) (15.0 * dn); return credits; } } *View
View.java:
package View;
import java.util.ArrayList; import javax.swing.JButton;
public class View {
public View() { }
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started