Could someone please fix the errors I have? //Abdullah.java public class Abdulla
ID: 3579725 • Letter: C
Question
Could someone please fix the errors I have?
//Abdullah.java
public class Abdullah {
//private instance variabels
private String firstName;
//Set first name
public void setA_name(String firstName){
this.firstName=firstName;
}
//Return name
public String getA_name(){
return firstName;
}
//print courses to consoles
public void print_Aclass(String courses[]){
for (int i = 0; i < courses.length; i++)
{
System.out.println("Course ["+(i+1)+"] = "+courses[i]);
}
}
//print total credit scores
public int getTotalCredits(int credits[]){
int total=0;
for (int i = 0; i < credits.length; i++) {
total+=credits[i];
}
return total;
}
//check if the number is even and returns true otherwise false
public boolean checkIdEven(int num){
return num%2==0;
}
//Abdullah_data.java
public class Abdullah_data {
public static void main(String[] args) {
//Set first name
Abdullah.setA_name("Abdullah");
//Print name to console
System.out.println("Name : "+abdullah.getA_name());
//Create a string array
String courses[]={"ifmg 210", "ifmg 250", "chem 107", "math 115", "econ 122"};
//Create an integer array of credits
int credits[]={3,3,3,3,3};
//print courses
abdullah.print_Aclass(courses);
//print total credits
System.out.println("Total Credits : "+Abdullah.getTotalCredits(credits));
System.out.println("10 Even ? "+Abdullah.checkIdEven(03));
}
}//end of class Abdullah_data
Explanation / Answer
Hi,
I have made some changes in both the classes.In the class Abdullah when you are writing a setter method you should never give type as void when it has a strign argument and you should always return the value for that.
Abdullah.java:
// Set first name
public String setA_name(String firstName) {
return this.firstName = firstName;
}
In Abdullah_data.java class,you should create an object for an instance method to be accessed as instance methods cannot be called directly.So the line of code changes like this:
Abdullah abdullah = new Abdullah();//create object for the class whose instance method you want to call
abdullah.setA_name("Abdullah");//call method on that object reference
Here goes your changed code:
Abdullah.java:
package com.chegg.test;
public class Abdullah {
// private instance variabels
private String firstName;
// Set first name
public String setA_name(String firstName) {
return this.firstName = firstName;
}
// Return name
public String getA_name() {
return firstName;
}
// print courses to consoles
public void print_Aclass(String courses[]) {
for (int i = 0; i < courses.length; i++) {
System.out.println("Course [" + (i + 1) + "] = " + courses[i]);
}
}
Abdullah_data.java:
package com.chegg.test;
public class Abdullah_data {
public static void main(String[] args) {
//you should create an object for an instance methods as instance methods cannot be called directly
// Set first name
Abdullah abdullah = new Abdullah();
abdullah.setA_name("Abdullah");
// Print name to console
System.out.println("Name : " + abdullah.getA_name());
// Create a string array
String courses[] = { "ifmg 210", "ifmg 250", "chem 107", "math 115", "econ 122" };
// Create an integer array of credits
int credits[] = { 3, 3, 3, 3, 3 };
// print courses
abdullah.print_Aclass(courses);
// print total credits
System.out.println("Total Credits : " + abdullah.getTotalCredits(credits));
System.out.println("10 Even ? " + abdullah.checkIdEven(03));
}
}// end of class Abdullah_data
Output:
Name : Abdullah
Course [1] = ifmg 210
Course [2] = ifmg 250
Course [3] = chem 107
Course [4] = math 115
Course [5] = econ 122
Total Credits : 15
10 Even ? false
Hope this helps you...
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.