What You Will Learn Layouts: the organization of graphical components inside a p
ID: 3752226 • Letter: W
Question
What You Will Learn Layouts: the organization of graphical components inside a panel. Deliverables app.java (initial application) MainFrame.java (external JFrame) ControlPanel, a Java class for the panel that will contain two other panels, using a layout. TopPanel, a Java class for the Panel that will display the group’s name and group's average GPA CenterPanel, a Java class for the Panel that will display names and semester GPAs of the 4 students in a group. group.java and student.java (a working version from previous labs, might need updates, see important #2 below). Students should apply consistent indenting in all submissions. This can be done via the NetBeans Source menu. Contents You can start with this NetBeans project. You will create 3 panels and one group object. One panel contains two other panels. On a top panel you will display the group's name and average GPA. The center panel will contain 4 buttons displaying the group 4 students name and GPA. Important - #1 - The single group object You will create only one group object g1 in this assignment. There will be only one statement group g1 = new group(...); in the whole application. Since two panels need to be used, g1 will need to be created somewhere and then pass the object g1 as a parameter to other classes. Important - #2 - GPA Calculation Your previous group/student solution might be working this way; otherwise, you will need to updated it. Because GPA is calculated randomly in student, depending how you calculate it there is a chance that the average group GPA will not match with the displayed sum of each student's GPA. In order to fix this, you need to: in group: semesterGPA is calculated using the GPA attribute in student, not the semesterGPA() method in student in student: it needs GPA as an attribute the attribute GPA is calculated in the constructor, when the student is created, calling the semesterGPA() random method the semesterGPA() method, whenever it is called, updates the value of the attribute GPA
Explanation / Answer
source code-1:
//myJFrame.java
import java.awt.*;
import javax.swing.*;
public class myJFrame extends JFrame
{
//Declare panel variable
private JPanel studentPanel;
//set width and height of the frame
private final int APPLICATION_WIDTH=450;
private final int APPLICATION_HEIGHT=550;
public myJFrame ()
{
super ("My First Frame");
//Call the method createStudentPanel that adds the student info
//to the panel add to the frame
createStudentPanel();
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(APPLICATION_WIDTH, APPLICATION_HEIGHT);
setVisible(true);
}
//The method creates an panel and instantiate student class object and
//add the student info that itself calls whatIsUp method
public void createStudentPanel()
{
//Create myJPanel
studentPanel = new myJPanel();
//Create an instance of student class
student std1=new student("Fred","Fonseca", 44);
//set layout flow layout
studentPanel.setLayout(new GridLayout(21,1));
JLabel name=new JLabel(std1.getName(),SwingConstants.CENTER);
name.setBackground(Color.gray);
//add a label
studentPanel.add(name);
//call whatsUp on std1 object and add
//the name info to the studentPanel 10 times
for (int index = 0; index < 20; index++)
{
JButton studentstatus=new JButton(std1.whatsUp());
studentPanel.add(studentstatus);
}
//add student panel to the frame
add(studentPanel);
}
}
------------------------------------------------------
//myJPanel.java
import java.awt.*;
import javax.swing.*;
public class myJPanel extends JPanel
{
public myJPanel ()
{
super ();
GridLayout grid = new GridLayout(1,1);
setLayout(grid);
setBackground(Color.yellow);
}
}
------------------------------------------------------
//Create a person class with first name and last name
public class person
{
private String informedFirstName;
private String informedLastName;
//constructor that sets the first name and last name
public person(String informedFirstName, String informedLastName)
{
this.informedFirstName=informedFirstName;
this.informedLastName=informedLastName;
}
//Returns the name of the person
public String getInfo()
{
return informedFirstName+" "+informedLastName;
}
}
------------------------------------------------------
//student.java
public class student {
private String firstName;
private String lastName;
private int age;
student(String informedFirstName, String informedLastName, int informedAge){
firstName = informedFirstName;
lastName = informedLastName;
age = informedAge;
}
student() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
String getName(){
return "NAME = "+firstName+ " "+ lastName+"Age = "+ age;
}
String getLastName(){
return lastName;
}
String getFirstName(){
return firstName;
}
String getFullName(){
return firstName+ " "+ lastName;
}
String getLastNameFirst(){
return lastName+ " "+ firstName;
}
String whatsUp(){
double myRandomNumber = Math.random();
double randomNumberTruncated = myRandomNumber*10+1;
int myRandomNumberToInt = (int) randomNumberTruncated;
String answer = " ";
switch(myRandomNumberToInt){
case 1:
answer = "is writing java";
break;
case 2:
answer = "searching the web";
break;
case 3:
answer = "Listening to endless lecture";
break;
case 4:
answer = "playing cards";
break;
case 5:
answer = "drinking beer";
break;
case 6:
answer = "eating peruvian food";
break;
case 7:
answer = "is talking on the phone";
break;
case 8:
answer = "is drawing";
break;
case 9:
answer = "is listening to music";
break;
case 10:
answer = "is sleeping";
break;
}
return answer;
}
}
------------------------------------------------------
//app.java
public class app
{
public static void main(String args[])
{
//Instantiate the myJFrame
myJFrame mjf = new myJFrame();
}
}
or
source code-2:
app.java
public class app
{
public static void main(String args[])
{
myJFrame mjf = new myJFrame();
}
}
myJFrame.java
import java.awt.*;
import javax.swing.*;
public class myJFrame extends JFrame
{
public myJFrame ()
{
super ("My First Frame");
myJPanel mjp = new myJPanel();
getContentPane().add(mjp,"Center");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize (640, 480);
setVisible(true);
}
}
myJPanel.java
import java.awt.*;
import javax.swing.*;
public class myJPanel extends JPanel
{
public myJPanel()
{
super();
setBackground(Color.gray);
setLayout(new BorderLayout());
myJPanel1 p1 = new myJPanel1();
myJPanel2 p2 = new myJPanel2();
add(p1,"North");
add(p2,"Center");
}
}
myJPanel1.java
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class myJPanel1 extends JPanel
{
JButton b1;
public myJPanel1()
{
super();
setBackground(Color.yellow);
b1 = new JButton("student info will be here later ...");
add(b1);
}
}
myJPanel2.java
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class myJPanel2 extends JPanel
{
JButton b2;
student st1;
public myJPanel2()
{
super();
st1 = new student("Fred","Fonseca",44);
setBackground(Color.pink);
b2 = new JButton("whats Up will be shown here" );
add(b2);
}
}
student.java
import java.awt.*;
import javax.swing.*;
public class student
{
String firstName;
String lastName;
int age;
public student(String a, String b, int x)
{
super();
firstName = a;
lastName = b;
age = x;
}
String getInfo()
{
return "NAME = "+firstName+ " "+lastName+" "+"Age = "+age;
}
String whatsUp()
{
double r = Math.random();
int myNumber = (int) (r * 3f); //comment: a random number between 0 and 2
String answer = "I don't know";
if(myNumber == 0) answer = "searching the web";
if(myNumber == 1) answer = "doing Java";
if(myNumber == 2) answer = "Listening to endless lecture";
return answer;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.