UML with subclasses - java

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.

Related

Class is not abstract and does not override abstract method problem

I tried hard to make it. But I've got an error message
"Student is not abstract and does not override abstract method compareTo(Object) in Comparable
class Student extends Person {"
abstract class Person implements Comparable {
private String name;
public Person(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
class Student extends Person {
private int id;
public Student(String name, int id) {
super(name);
this.id = id;
}
public String toString() {
return Integer.toString(id);
}
public int getId() {
return this.id;
}
#Override
public int compareTo(Student s) {
if (this.id < s.getId()) {
return -1;
}else if (this.id > s.getId()) {
return 1;
}
return 0;
}
}
#Override
public int compareTo(Student s) {
if (this.id < s.getId()) {
return -1;
}else if (this.id > s.getId()) {
return 1;
}
return 0;
}
this is where I think having a problem...
As already explained by #Andreas, make Student implements Comparable, not Person.
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
abstract class Person {
private String name;
public Person(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
class Student extends Person implements Comparable<Student> {
private int id;
public Student(String name, int id) {
super(name);
this.id = id;
}
public String toString() {
return Integer.toString(id);
}
public int getId() {
return this.id;
}
#Override
public int compareTo(Student o) {
return Integer.compare(this.id, o.id);
}
}
Demo
public class Main {
public static void main(String[] args) {
List<Student> list = new ArrayList<Student>();
list.add(new Student("Abc", 321));
list.add(new Student("Xyz", 12));
list.add(new Student("Mnp", 123));
Collections.sort(list);
System.out.println(list);
}
}
Output:
[12, 123, 321]

How do I Solve this method?

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?

my variable cannot be resolved into a type in java

It keeps saying that 'Cow' cannot be resolved to a type. I am new at java and would like some help!
public class UnoMas {
public static void main(String[] args) {
Cow c;
c = new Cow("Gerard, Golgari Lich Lord");
c.setPower(0);
c.setToughness(0);
speakToTheGolgari(c);
System.out.println(c.getPower);
System.out.println(c.getToughness);
}
public static void speakToTheGolgari(Cow d) {
d.setPower(d.getPower() + 2);
d.setToughness(d.getToughness() + 2);
}
}
I'm assuming that this is a custom class that you're meant to create?
Since you're new to Java I'm also assuming that you didn't know that you're supposed to create it?
In any event create your 'Cow' class as per below (feel free to remove redundant constructors):
public class Cow {
private String name;
private int power;
private int toughness;
public Cow() {
}
public Cow(String name) {
this.name = name;
this.power = power;
this.toughness = toughness;
}
public Cow(String name, int power) {
this.name = name;
this.power = power;
this.toughness = toughness;
}
public Cow(String name, int power, int toughness) {
this.name = name;
this.power = power;
this.toughness = toughness;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPower() {
return power;
}
public void setPower(int power) {
this.power = power;
}
public int getToughness() {
return toughness;
}
public void setToughness(int toughness) {
this.toughness = toughness;
}
}

Passing objects as parameter to method JAVA

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.

I am looking to set and an array in java while setting a sub method in the array

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);

Categories

Resources