Trouble calling methods from a class/ [closed] - java

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 11 months ago.
Improve this question
Can someone tell me whats wrong with these codes ?
public class Student {
String name;
int roll_no;
public void getDetails(String Name, int roll) {
name = Name;
roll_no = roll;
}
}
and this
public class StudentRun {
Student student = new Student();
String n = "John";
int r = 2;
student.getDetails(n, r);
}
It shows the error:
Multiple markers at this line
on the line where i call the student.getDetails(n,r)

You cannot call a method in a class without it being wrapped in a method.
Your Student class also lacks a constructor (a method that is called when the class is instantiated) and lacks the context of attribute visibility (public/protected/private).
The constructor must call itself as the class, in your case:
public class Student {
protected String name;
protected int roll_no;
public Student(String Name, int roll) {
this.name = Name;
this.roll_no = roll;
}
public String getName() {
return this.name
}
....
}
Once you have structured the class correctly, you need to do the following to instantiate it:
class OtherClass {
public static void main (String[] args) {
student = new Student("John", 42);
System.out.println(student.getName());
}
}

it looks to me you are calling the method without wraping it in a method in your class StudentRun. try using a constructor or some other method to call student.GetDetails
like
void callStudentRun
{
student.Getdetails();
}

Related

Write a constructor in class Doctor to initialize the private field doctor_name [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 months ago.
Improve this question
Write a constructor in class Doctor to initialize the private field doctor_name. How can I write this?
class Doctor{
public Doctor(private String doc_name){
}
}
Here is an example class Doctor with a constructor.
class Doctor{
private String doc_name;
Doctor(String doc_name){
this.doc_name = doc_name;
}
}
You can also add a setter and getter to apply encapsulation.
class Doctor{
private String doc_name;
Doctor(String doc_name) {
this.doc_name = doc_name;
}
public void setDoctorName(String doc_name) {
this.doc_name = doc_name;
}
public String getDoctorName() {
return this.doc_name;
}
}
I've made a simple implementation of the class Doctor.
class Main {
public static void main(String[] args) {
Doctor doctor1 = new Doctor("Someone");
System.out.println(doctor1.getDoctorName());
doctor1.setDoctorName("Someone Else");
System.out.println(doctor1.getDoctorName());
}
}
Output:
Someone
Someone Else

How to call a method from another class, in a class in Java [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I'm trying to use a method I've created in class Student, in another class, Classroom. I'm wondering if it is even possible to call a method that's defined in another class, or if I'm approaching this the wrong way.
classStudent:
public class Student {
String firstName;
Student(String firstName){
this.firstName=firstName;
}
public String getFirstName() {
return firstName;
}
}
class Classroom:
public class Classroom{
public ArrayList<Student> students;
public Classroom(ArrayList<Student> studentArrayList) {
}
Student getStudent(ArrayList students){
this.students=students;
System.out.println(students.get(0).getFirstName);
return null;
}
}
Main:
public class Main {
public static void main(String[] args) {
Student a = new Student("John");
Student b = new Student("Jane");
ArrayList<Student> studentArrayList = null;
studentArrayList.add(a);
studentArrayList.add(b);
Classroom c = new Classroom(studentArrayList);
System.out.println(c.getStudent(studentArrayList));
}
}
My problem seems to be when I attempt to call the method getFirstName, I'm wondering is there is a way to call getFirstName in the Classroom class. My intention for this code is to return the object a.
The problem is, you forgot to put parentheses after getFirstName() in the Classroom class, also, you should initialize students in the constructor.
Here's what it should look like:
public class Classroom {
public ArrayList<Student> students;
public Classroom(ArrayList<Student> studentArrayList) {
students = studentArrayList
// you can add other code here
}
Student getStudent(int index) {
// get the first name of the student at the given index
System.out.println(students.get(index).getFirstName());
return students.get(index); // return the Student object instead of null
}
}
Then, inside the Main class:
public class Main {
public static void main(String[] args) {
Student a = new Student("John");
Student b = new Student("Jane");
ArrayList<Student> studentArrayList = new ArrayList<Student>();
studentArrayList.add(a);
studentArrayList.add(b);
Classroom c = new Classroom(studentArrayList); // now the students ArrayList is initialized
System.out.println(c.getStudent(0)); // replace 0 with the index you want
}
}
You can keep the Student class as is.
students.get(0).getFirstName is a field.
To call the method just add parentheses - students.get(0).getFirstName()

Testing an Interface with jUnit [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I have an Interface and a class which implements it.
I #Override the method and I want to test it using JUnit4.
I don't know if I have an error on my method or if I'm doing something wrong in my test class.
Is it correct to have an instance of the interface itself, or should I make it an instance of the myCustomString Class which implements the Interface.
if anyone could explain how the objects are calling each other that would be awesome.
My JUnit test returns a null pointer exception.
public interface MyCustomStringInterface {
int countNumbers();
}
public class MyCustomString implements MyCustomStringInterface {
private String string;
#Override
public int countNumbers() {
while (string.length() != 0 ) {
int count = 0;
for (int i = 0; i < string.length(); i++) {
if(Character.isDigit(string.charAt(i))) count++;
}
return count;
}
return 0;
}
public class MyCustomStringTest {
private MyCustomStringInterface mycustomstring;
#Test
public void testCountNumbers1() {
mycustomstring.setString("th1s strin8 shou1d hav3 numb3rs,
righ7?");
assertEquals(6, mycustomstring.countNumbers());
}
}
Well, according to the code you posted, you didn't create a MyCustomString object. That's probably the source of your NullPointerException.
Try adding mycustomstring = new MyCustomString() right above your setString method, like this:
public class MyCustomStringTest {
private MyCustomStringInterface mycustomstring;
#Test
public void testCountNumbers1() {
mycustomstring = new MyCustomString();
mycustomstring.setString("th1s strin8 shou1d hav3 numb3rs, righ7?");
assertEquals(6, mycustomstring.countNumbers());
}
}

Can't get my method to return anything [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
So, this is a class where I store data from another class called (Name)
the class (Name) got 3 informations. Name (String)/Number List /Adress List I used the last 2 as a List so I can allow multiple adresses/numbers
now the thing is, i can't get my getContact in this code to work, it doesn't return anything, so i thought that i messed up somewhere, checked everything and things were right, so i bypassed toString and printed an object of the type Namebook is my main method and it worked just fine.
public class Namebook {
private ArrayList<Name> contact;
private Name ctc;
public Namebook(){
contact = new ArrayList<Name>();
}
public void addContact(Name name){
this.contact.add(name);
}
public String getContact(){
return String.valueOf(this.contact);
}
#Override
public String toString() {
return String.valueOf(this.contact);
}
}
Class (Name)
public class Name {
private String name;
private List<String> number;
private List<String> Adress;
public Name(String name){
this.name = name;
}
public void addNumber(List<String> num){
this.number = num;
}
public void addAdress(List<String> adress){
this.Adress = adress;
}
public List<String> getNumber(){
return this.number;
}
public List<String> getAdress(){
return this.Adress;
}
public String toString() {
return this.name + " " + getNumber() + " " + getAdress() ;
}
}
Main
public class Main {
Scanner reader = new Scanner(System.in);
public static void main(String[] args) throws Exception {
Name person = new Name("sacha");
ArrayList<String> add = new ArrayList<String>();
ArrayList<String> num = new ArrayList<String>();
add.add("chicago");
num.add("13213223");
person.addNumber(num);
person.addAdress(add);
//System.out.println(person);
Namebook p1 = new Namebook();
p1.addContact(person);
p1.getContact();
}
}
Your toString and getContact methods are identical, and behave identically.
Case 1:
System.out.println(person);
This is short for
System.out.println(person.toString());
Case 2:
p1.getContact();
This is not short for anything. But notice that you do not have a System.out.println statement. That is what prints the output of the method. Without that, nothing is printed. To fix it
System.out.println(p1.getContact());

Allocate more than one value to an Array? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
i'm new to Java and I'm trying to write a code that Scan for some Patients' names, also ask them to enter (y/n) if they are allergicToGluten, and then I print out a list of name of all the patients that are allergicToGluten. I just learnt Arrays but i'm still struggling in assigning many values ( name + y/n ) to an Array. I need your help.
Thank you very much :D
You need to create a class that represents your patient. And then you can have array of patients
public class Patient{
private String name;
private boolean allergicToGluten;
public Patient(String name, boolean allergicToGluten){
this.name = name;
this.allergicToGluten = allergicToGluten;
}
public boolean isAllergicToGluten(){
return allergicToGluten;
}
public String getName(){
return name;
}
}
----
Patient[] patients = new Patient[patientCount];
If you don't know patientCount then you need resizable-array.
ArrayList<Patient> patients = new ArrayList<Patient>();
// ... reading 'name' and 'isAllergic' from input
patients.add(new Patient(name, isAllergic));
And then you can print list of allergic patients
for(p : patients){
if (p.isAllergicToGluten())
System.out.println(p.getName());
}
There is no way to allocate more than one value to an array, but you can make an array of some class with multiple fields in it. For example,
public class Patient {
public String name;
public boolean isAllergic;
public Patient(String name, boolean isAllergic) {
this.name = name;
this.isAllergic = isAllergic;
}
}
public class Patient_Driver {
public static void main(String[] args) {
Patient[] patients = new Patient[] {
new Patient("Steve", true),
new Patient("Mary", false)
};
for (int i = 0; i < patients.length; i++) {
if (patients[i].isAllergic) {
System.out.println(patients[i].name);
}
}
}
}
Output:
Steve

Categories