Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

there is only one version of a student. student information is shown in the uppe

ID: 3852614 • Letter: T

Question

there is only one version of a student.

student information is shown in the upper button (in myJPanel1).

when the user clicks on the upper button in myJPanel1, then WhatsUp() is shown in the lower button (in myJPanel2).

The difference here is that you are required to:

You have to change two or three lines in the code of myJPanel (the panel that creates the two other panels)

A lot of changes in the Java code of myJPanel1

NO changes in the code of myJPanel2 (of course panel2 graphics will change to display the whatsUp())

GIVEN CODE

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("Communication Between Classes - Assignment");

myJPanel mjp = new myJPanel();
add(mjp, "Center");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(640, 480);
setVisible(true);
}

}

myJPanel.java


import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class myJPanel extends JPanel
{

public myJPanel()
{
super();
setBackground(Color.gray);

setLayout(new BorderLayout());
myJPanel1 p1 = new myJPanel1();
myJPanel2 p2 = new myJPanel2();
student st1 = new student("Kerry","Collins",20);
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;

public myJPanel2()
{
super();
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;
}

}

Explanation / Answer

Answer:

Changes in myJPanel.java so that myJPanel2 implement a listener for myJPanel1

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(p1);      
              add(p1,"North");
              add(p2,"Center");       
   }
}

Here is myJPanel2 that implement a listener for a button in the first panel

myJPanel2.java

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class myJPanel2 extends JPanel implements ActionListener
{
   JButton b2;
   student st1;

   myJPanel1 mJP1;
   public myJPanel2(myJPanel1 panel1)
   {
               super();
               st1 = new student("Fred","Fonseca",44);
               setBackground(Color.pink);

mJP1=panel1;

     b2 = new JButton("whats Up will be shown here" );

              mJP1.b1.addActionListener(this);
              add(b2);      
   }

public void ActionPerformed(ActionEvent aEve)

{

b2.setText(st1.whatsUp());

}

}

The Complete Code is as follows,

1.app.java

public class app
{
   public static void main(String args[])
   {   
        myJFrame mjf = new myJFrame();

   }
}

2.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);
   }

}

3. 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(p1);      
              add(p1,"North");
              add(p2,"Center");       
   }
}

4.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);
          }      
}

5. myJPanel2.java

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class myJPanel2 extends JPanel implements ActionListener
{
   JButton b2;
   student st1;

   myJPanel1 mJP1;
   public myJPanel2()
   {
               super();
               st1 = new student("Fred","Fonseca",44);
               setBackground(Color.pink);
               b2 = new JButton("whats Up will be shown here" );

              mJP1.b1.addActionListener(this);
              add(b2);      
   }

public void ActionPerformed(ActionEvent aEve)

{

b2.setText(st1.whatsUp());

}

}

6.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;
          }  

}