JAVA Question: 1. S.upp.ose that you a.r.e writ.ing a clas.s named St.ude.nt. It
ID: 3766367 • Letter: J
Question
JAVA Question:
1. S.upp.ose that you a.r.e writ.ing a clas.s named St.ude.nt. It has thre.e private attri.butes: int id; Stri.ng name; and double gpa; Wr.ite a c.on.s.tru.ctor with thr.ee arg.umen.ts for this St.udent class.
2. For sam.e Stud.ent class in Pr.ob.lem 1, wri.te set and get method for attr.ibute g.p.a.
3. For same Student class in Prob.lem1, write one line of code to create a Student object named student1. Student1’s name is “John Doe”; id is 1234; and gpa is 3.92
4. Wr.ite one lin.e of code to dis.pla.y a message “Thank You!”. You m.ust use method in JOptionPane class to do so.
5. Write one li.ne of code to make the JTextFiled displayField cannot be typed in.
6. For sa.me Stu.dent c.lass in Problem .1, override eq.uals meth.od inhe.rited from Obje.ct class. Two Student object.s are equal .if they have the same id.
7. Su.ppose that you hav.e a class Emplo.yee, which has three priv.ate attribu.tes: Stri.ng id; String na.me; and String address; Suppo.se class HourlyEm.ployee is a child class of Employee. Hou.rlyEmp.loyee has an at.tribute dou.ble wage;
a. Write. the hea.der of HourlyEmployee class.
b. Write necessa.ry m.utator and accessor me.thods for (HourlyEmployee class)
PLEASE JUST Ignore dots...except for the dot in bolded letters :)
Explanation / Answer
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package problem1;
/**
*
* @author TANAY
*/
public class Student extends javax.swing.JFrame {
private
int id;
String name;
double gpa;
public
Student(int i, String s, double g)
{
id=i;
name=s;
gpa=g;
initComponents();
}
void set(double g)
{
gpa=g;
}
double get()
{
return gpa;
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jOptionPane1 = new javax.swing.JOptionPane();
jTextField1 = new javax.swing.JTextField();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
addFocusListener(new java.awt.event.FocusAdapter() {
public void focusGained(java.awt.event.FocusEvent evt) {
form_load(evt);
}
});
jTextField1.setText("jTextField1");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(123, 123, 123)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(218, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(137, 137, 137)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(143, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void form_load(java.awt.event.FocusEvent evt) {
// TODO add your handling code here:
jOptionPane1.showMessageDialog(null, "Thank you");
jTextField1.disable();
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Student.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Student.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Student.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Student.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
Student student1 = new Student(1234,"John Doe",3.92);
student1.setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JOptionPane jOptionPane1;
private javax.swing.JTextField jTextField1;
// End of variables declaration
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package problem1;
/**
*
* @author TANAY
*/
public class Employee extends javax.swing.JFrame {
/**
* Creates new form Employee
*/
private
String id;
String name;
String address;
public Employee() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);
pack();
}// </editor-fold>
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Employee.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Employee.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Employee.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Employee.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Employee().setVisible(true);
}
});
}
// Variables declaration - do not modify
// End of variables declaration
}
class HourlyEmployee extends Employee
{
double wage;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.