I was given the main coding to do the sub class .
but I was stuck in these coding as shown at the below :
z.setName(z.obj1);
z.setID(z.obj2);
********** It should be the way to insert input. **************
========== The subClass I shown at the below was written by myself ==========
The main coding given as below :
public static void main(String[] args) {
StudReg z = new StudReg();
z.setName(z.obj1);
z.setID(z.obj2);
System.out.println(z.getName());
System.out.println(z.getID());
System.out.println(z.getJava());
System.out.println(z.getDatabase());
StuComputing obj3,obj4;
obj4 = new StuComputing();
obj4.setStudReg(z);
System.out.println(obj4.GPA().getGPA());
}
The subClass (StudReg) I've done as below :
public class StudReg {
//Data Member
String Name;
String ID;
double Java,Database;
//Constructor
public StudReg(){};
public StudReg(String a,String b){
Name = a;
ID = b;
};
//Name
public void setName(String n){
Name = n;
}
public String getName() {
return Name;
}
//Id
public void setID (String i){
ID = i;
}
public String getID (){
return ID;
}
//Java
public void setJava (double j){
Java = j;
}
public double getJava (){
return Java;
}
//Database
public void setDatabase (double d){
Database = d;
}
public double getDatabase (){
return Database;
}
//FUNCTION
public StudReg (StudReg gg){
double aa,bb;
//refer to data from MAIN
aa = this.getJava();
bb = this.getDatabase();
}
Another SubClass - StuComputing:
public class StuComputing {
//DATA MEMBER
public StudReg ss;
double GPA;
//CONSTRUCTOR
public StuComputing (){};
public StuComputing (double a1){
GPA = a1;
};
//StudReg
public void setStudReg (StudReg st){
ss = st;
}
public StudReg getStudReg(){
return ss;
}
//GPA
public void setGPA(double g){
GPA = g;
}
public double getGPA(){
return GPA;
}
Beside answer my problem, can you all show a simple example ?
So, I can understand it easily ><
Thanks
I have still no idea what you really want to do... try this
class StudReg {
String obj1 ;
String obj2 ;
double java ;
double database ;
String name ;
String id ;
public double getJava() {
return java;
}
public double getDatabase() {
return database;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getID() {
return id;
}
public void setID(String id) {
this.id = id;
}
public String getObj1() {
return obj1;
}
public void setObj1(String obj1) {
this.obj1 = obj1;
}
public String getObj2() {
return obj2;
}
public void setObj2(String obj2) {
this.obj2 = obj2;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
class StuComputing {
StudReg studReg;
GPA gpa;
public StuComputing() {
gpa = new GPA();
}
public StudReg getStudReg() {
return studReg;
}
public void setStudReg(StudReg sr) {
this.studReg = sr;
}
public GPA GPA() {
return gpa;
}
}
public class GPA {
double gpa;
public GPA() {
}
public double getGPA(){
return gpa;
}
}
You must have forgotten something. there is no "obj1" or "obj2" attribute in your class StudReg.
So how can we understand your problem? I think you need to ask your question again in other wording.
Related
I have 3 Classes:Account,Customer and Main.the Main Class has the main method:
these Classes are the some parts of a programm.
public class Account {
private static ArrayList<Account> allAccounts=new ArrayList<>();
private Bank bank;
private int id;
private int money;
private int remainingDuration;
private int interest;
private Customer customer;
public Account(Bank bank, Customer customer,int id, int money,int duration,int interest) {
this.bank = bank;
this.customer=customer;
this.id=id;
this.money = money;
this.remainingDuration=duration;
this.interest = interest;
allAccounts.add(this);
}
public int getId() {
return id;
}
public Bank getBank() {
return bank;
}
}
public class Customer {
private static ArrayList<Customer> allCustomers=new ArrayList<>();
private String name;
private double moneyInSafe;
private ArrayList<Account> allActiveAccounts;
private int totalNumberOfAccountsCreated;
private int negativeScore;
public Customer(String name, double moneyInSafe) {
this.name=name;
this.moneyInSafe=moneyInSafe;
totalNumberOfAccountsCreated=0;
allCustomers.add(this);
}
public static Customer getCustomerByName(String name){
for (Customer customer:allCustomers){
if(customer.getName().equals(name)){
return customer;
}
}
return null;
}
public String getName() {
return name;
}
public void createNewAccount(Bank bank,int money,int duration,int interest){
totalNumberOfAccountsCreated++;
allActiveAccounts.add(new Account(bank,this, totalNumberOfAccountsCreated, money, duration, interest));
}
public double getMoneyInSafe() {
return moneyInSafe;
}
public void setMoneyInSafe(double moneyInSafe) {
this.moneyInSafe = moneyInSafe;
}
public boolean hasActiveAccountBank(Bank bank){
}
private Account getAccountWithId(int id){
for(Account account:allActiveAccounts){
if(account.getId()==id){
return account;
}
}
return null;
}
}
public class Bank {
private static ArrayList<Bank> allBanks=new ArrayList<>();
private String name;
public Bank(String name) {
this.name = name;
allBanks.add(this);
}
public static Bank getBankWithName(String name){
for (Bank bank:allBanks){
if(bank.getName().equals(name)){
return bank;
}
}
return null;
}
public static boolean isThereBankWithName(String name){
return allBanks.contains(getBankWithName(name));
}
public static int getAccountInterestFromName (String name){
if(name.equals("KOOTAH")){
return 10;
}else if(name.equals("BOLAN")){
return 30;
}else{
return 50;
}
}
public String getName() {
return name;
}
}
So my question is How do I Define the hasActiveAccountBank method in Customer Class to Check Is there any Account with this Account id or not in Main Class.
the part of the Main Class has a matcher that returns Customer's name and the id so they are given.Here is the part:
if (!getCustomerByName(matcher.group(1)).hasActiveAccountBank()) {
System.out.println("Chizi zadi?!");
}
So How do i Write in the hasActiveAccountBank() argument?
So I have a student class.
I have a Jlist and try to add a student object. I set dob value.
Everything runs fine no issue. When I retrive sout "student" it returns all other values, but not Dob.
When I call student.getDob() in fact it returns, means it's assigned.
Don't know why it doesn't retrieve in student as an object. I am thinking maybe there is a bug in netbeans.
Any help?
private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {
// Create an instance of student
Student student = new Student();
student.setFName(firstNameField.getText());
student.setLName(lastNameField.getText());
student.setDob(dateOfBirthField.getText());
student.setGender((String) genderComboBox.getSelectedItem());
System.out.println(student.getDob());
// Add student to the list
ListModel lModel = jList1.getModel();
DefaultListModel dlm = new DefaultListModel<Object>();
int total = lModel.getSize();
for (int x = 0; x < total; x++) {
dlm.addElement(lModel.getElementAt(x));
}
dlm.addElement(student);
jList1.setModel(dlm);
//Clearing the text fields for the next entry
firstNameField.setText("");
lastNameField.setText("");
dateOfBirthField.setText("");
genderComboBox.setSelectedIndex(0);}
}
Student Class:
import java.util.ArrayList;
import java.util.Date;
public class Student {
private String id;
private String fName;
private String lName;
private String gender;
private String dob;
private ArrayList<Address> addresses;
private Instructor instructor;
private ArrayList<Courses> courses;
public Student () {
}
public ArrayList<Address> getAddresses () {
return addresses;
}
public void setAddresses (ArrayList<Address> val) {
this.addresses = val;
}
public ArrayList<Courses> getCourses () {
return courses;
}
public void setCourses (ArrayList<Courses> val) {
this.courses = val;
}
public String getDob () {
return dob;
}
public void setDob (String val) {
this.dob = val;
}
public String getFName () {
return fName;
}
public void setFName (String val) {
this.fName = val;
}
public String getGender () {
return gender;
}
public void setGender (String val) {
this.gender = val;
}
public String getId () {
return id;
}
public void setId (String val) {
this.id = val;
}
public Instructor getInstructor () {
return instructor;
}
public void setInstructor (Instructor val) {
this.instructor = val;
}
public String getLName () {
return lName;
}
public void setLName (String val) {
this.lName = val;
}
#Override
public String toString(){
return getFName()+" "+getLName()+" "+getGender();
}
}
I found it with deep debugging. In the student class Dob was not stringified. Added getDob() at the end.
#Override
public String toString(){
return getFName()+" "+getLName()+" "+getGender()+" "+getDob();
}
public class Student {
private String name;
private long id;
private String grade;
private int[] test;
private int NUM_TESTS;
public Student(){
name="Un";
id=0;
grade="Un";
test=new int[0];
NUM_TESTS=5;
}
public Student(String x, long z) {
name=x;
id=z;
}
public void setName(String n) {
name=n;
}
public void setID(long i) {
id=i;
}
public void setGrade(String g) {
grade=g;
}
/*public void setTestScore(int t,int s) {
test=t;
test=s;
}
public int getTestScore(int) {
return test;
}*/
public int getNumTests() {
return NUM_TESTS;
}
public String getName() {
return name;
}
public long getID() {
return id;
}
public String getGrade() {
return grade;
}
public String toString() {
return getTestScore()+getNumTests()+getName()+getID()+getGrade();
}
/*public void calculateResult() {
int sum=0;
for (int t:test)sum+=t;
double average= 1.0t*sum/5;*/
}
}
Here is my code I have spaced out the places where I am having the issues. I am writing a Student subclass with subclasses undergrad and postgrad.
Here is the UML
I don't understand how to correctly implement testScore if it is not one of the variables? Nevermind the calculate result I'll fix that myself. I am also unsure if my constructors are accurate. All the students do five exams that's a constant
setTestScore(int t, int s)... I do recommend to use carefully chosen names (identifiers). For example if you just rename the parameters to: setTestScore(int testNumber, int score) you can be more familiar what should you inplement.
test = new int[0];isn't what you want. You want test = new int[NUM_TESTS]
Try to reconsider method setTestScore(int testNumber, int score)
first parameter is actually the index in the array of test and the second is the value.
So, your method should be something like this:
public void setTestScore(int testNumber, int score) {
test[testNumber] = score;
}
I just gave you some guidance for your own implementation...
First of all, It seems that Student class should be abstract. because each student is UnderGraduate or PostGraduate.
Secondly, you should extend the child classes from Student class.
I hope the below code be helpful:
abstract class Student {
private String name;
private long id;
private String grade;
private int[] test;
private final int NUM_TESTS = 5;
public Student(){
name = "UN";
id = 0;
grade = "UN";
test = new int[NUM_TESTS];
}
public Student(String name, long id){
this.name = name;
this.id = id;
}
#Override
public String toString() {
//TODO: write your desire toString method
return getNUM_TESTS()+getName()+getId()+getGrade();
}
abstract void claculateResult();
public int getTestScore(int testNumber){
if(testNumber >= NUM_TESTS)
return 0;
return test[testNumber];
}
public void setTestScore(int testNumber, int score){
if(testNumber >= NUM_TESTS)
return;
test[testNumber] = score;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getGrade() {
return grade;
}
public void setGrade(String grade) {
this.grade = grade;
}
public int[] getTest() {
return test;
}
public void setTest(int[] test) {
this.test = test;
}
public int getNUM_TESTS() {
return NUM_TESTS;
}
}
and the UnderGraduate class would be:
public class UnderGraduate extends Student{
public UnderGraduate(){
}
public UnderGraduate(String name, long id){
super();
}
#Override
void claculateResult() {
//TODO: DO whatever you want
}
}
remember that the PostGraduate class is same as UnderGraduate.
Hi so I have a project for college where I need to build a chat in java that to work only in on computer where you execute the code and you give the name of the two users and a translation value but I am having a problem with the way they want me to do it since I cant use super, arrays , extends, or inherences I am having a problem how it currently is I my Main like this
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Chat ch = null;
boot(in, ch);
menu(in, ch);
}
public static Chat boot (Scanner in, Chat ch){
String name1;
String name2;
int transvalue = 0;
System.out.print("Username 1:");
name1=in.nextLine();
do{
System.out.print("Username 2:");
name2=in.nextLine();
if (name1.equals(name2)){
System.out.println("Names can't be equal");
}
}while(name1.equals(name2));
do{
System.out.print("Translation Value:");
transvalue=in.nextInt();
in.nextLine();
if (transvalue<1 || transvalue>26){
System.out.println("Value Can only be between 0 and 26");
}
}while(transvalue<1 || transvalue>26);
return new Chat(name1,name2,transvalue);
}
So this show be adding the info I put in to my class Chat
public class Chat {
private static final int id1 = 1;
private static final int id2 = 2;
private Users u1;
private Users u2;
private Conversation conv;
public Chat(String name1, String name2, int transvalue){
u1 = new Users(id1,name1);
u2 = new Users(id2,name2);
conv = new Conversation(transvalue);
}
public static int getId1() {
return id1;
}
public static int getId2() {
return id2;
}
public Users getU1() {
return u1;
}
public void setU1(Users u1) {
this.u1 = u1;
}
public Users getU2() {
return u2;
}
public void setU2(Users u2) {
this.u2 = u2;
}
public Conversation getConv() {
return conv;
}
public void setConv(Conversation conv) {
this.conv = conv;
}
}
but when I add the info and try to return it like for example:
System.out.print(ch.getU1());
or
System.out.print(ch.getU1().getName());
I am getting a null am I doing something wrong?
public class Users {
private int id ;
private String name;
private String mess;
public Users(int id,String name){
this.id=id;
this.name=name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
Can anyone find or point out what I'm doing wrong
Thank you in Advance to all that take the time to Help
I have seen other array codes but here is what I got. I am trying to set the courseID in the CollegeCollege course in the array. I need to set the array in the student.java
public class CollegeCourse {
private static String courseID;
private static int creditHours;
private static char grade;
public static void setCourseID(String course){
courseID = course;
}
public static String getCourseID(){
return courseID;
}
public static void setCreditHours(int CH){
creditHours = CH;
}
public static int getCreditHours(){
return creditHours;
}
public static void setGrade(char g){
grade = g;
}
public static char getGrade(){
return grade;
}
public class Student {
private static int IDnumber;
private static CollegeCourse[] course = new CollegeCourse[5];
public static void setIDnumber(int x){
IDnumber = x;
}
public static int getIDnumber(){
return IDnumber;
}
public static String getCourse(int x){
return course[x].getCourseID();
}
public static void setCourse(CollegeCourse newCourse, int ID){
CollegeCourse.setCourseID = newCourse[ID];
}
}
It could seem like you are trying to code that the student takes course with id 5. Maybe your classes should have constructors taking arguments based on what ID the course has.
and I don't know what you need the fields to be static for, bt feel free to have them static if it is needed for some reason.
public class CollegeCourse {
private String courseID;
private int creditHours;
private char grade;
public CollegeCourse(String courseID) {
this.courseID = courseID;
}
...
public class Student {
...
private static CollegeCourse[] course = new CollegeCourse("5");
...
I think a better design would be this:
public class CollegeCourse {
private final String courseId;
private final int creditHours;
public CollegeCourse(final String courseId, int creditHours) {
this.courseId = courseId;
this.creditHours = creditHours;
}
public String getCourseId() {
return courseId;
}
public int getCreditHours() {
return creditHours;
}
#Override
public boolean equals(Object o) {
if (o == this) { return true; }
if (!(o instanceof CollegeCourse)) { return false; }
CollegeCourse cc = (CollegeCourse)o;
return courseId.equals(cc.courseId)
&& creditHours == cc.creditHours;
}
#Override
public int hashCode() {
int result = 17;
result = 31 * result + courseId.hashCode();
result = 31 * result + creditHours;
return result;
}
};
public enum Grade { A, B, C, D, E };
public class Student {
private final String id;
private final Map<CollegeCourse, Grade> course2GradeMap = new HashMap<>();
public Student(final String id) {
this.id = id;
}
public Grade getGrade(final CollegeCourse course) {
return couse2GradeMap.get(course);
}
public void addCollegeCourse(final CollegeCourse course, Grade grade) {
course2GradeMap.put(course, grade);
}
public Collection<CollegeCourse> getCollegeCourses() {
return Collections.unmodifiableCollection(course2GradeMap.values());
}
};
Use it in this way:
Student student = new Student("001");
student.addCollegeCourse(new CollegeCourse('alg', 100), Grade.A);
student.addCollegeCourse(new CollegeCourse('stat', 100), Grade.C);