+ 1

Please someone tell me what's wrong with this code

https://code.sololearn.com/c1O9rHwRC1G1/?ref=app

22nd Mar 2018, 3:26 PM
Devansh Gupta
Devansh Gupta - avatar
11 Answers
+ 3
@Devansh CHANGE: class program TO: public class program ^If you do that, it should work. It's a scope issue and your main loop was contained inside of a non-public class. However, as I mentioned before, I would take some time to properly learn to set up class/objects. Initially you were going about it with the right thought, using getters/setters/other class methods, but you never created those methods and now are choosing to not use them at all. They're really quick/simple to set up once you learn the setup of them, but most importantly, they secure your classes and hides your class members from the rest of the program. As well, if you ever find yourself working on a team or in a corporate/business setting, going about things in a sloppy way won't be tolerated; that's another reason why it's good to stick with common practices/standards. Anyways, I wish you the best of luck, Devansh! If you have further issues or questions, don't hesitate to ask. Take care!
23rd Mar 2018, 1:04 PM
Fata1 Err0r
Fata1 Err0r - avatar
+ 4
Here is part 2. It's the code in the main loop. https://code.sololearn.com/cen6eORJRWTw/#java :::JAVA - MAIN LOOP::: Student s1 = new Student(); Student s2 = new Student("Suraj", 191, "Ok College"); System.out.println("Student 1's Name: " + s1.getStudentName()); System.out.println("Student 1's ID Number: " + s1.getStudentID()); System.out.println("Student 1's College Name: " + s1.getSchoolName()); System.out.println(""); System.out.println("Student 2's Name: " + s2.getStudentName()); System.out.println("Student 2's ID Number: " + s2.getStudentID()); System.out.println("Student 2's College Name: " + s2.getSchoolName()); System.out.println(""); System.out.print("Making changes to Student 1's records...."); s1.setStudentName("Jakob"); s1.setStudentID(420); s1.setSchoolName("University of Georgia"); System.out.println("done."); System.out.println(""); System.out.println("Student 1's Name: " + s1.getStudentName()); System.out.println("Student 1's ID Number: " + s1.getStudentID()); System.out.println("Student 1's College Name: " + s1.getSchoolName()); System.out.println("");
22nd Mar 2018, 4:22 PM
Fata1 Err0r
Fata1 Err0r - avatar
+ 3
Here you go. I rewrote your code for you and placed comments to help explain how to create a basic class in Java and how to utilize its methods. Hope this helps! If you run into any problems, let me know. https://code.sololearn.com/cen6eORJRWTw/#java ::::: OUTPUT :::::: Student 1's Name: John Doe Student 1's ID Number: -1 Student 1's College Name: N/A Student 2's Name: Suraj Student 2's ID Number: 191 Student 2's College Name: Ok College Making changes to Student 1's records....done. Student 1's Name: Jakob Student 1's ID Number: 420 Student 1's College Name: University of Georgia
22nd Mar 2018, 4:20 PM
Fata1 Err0r
Fata1 Err0r - avatar
+ 3
Since I couldn't post the code into one post, I'll make two posts. One for the class and the other for the main loop's class. https://code.sololearn.com/cen6eORJRWTw/#java :::JAVA - STUDENT CLASS::: class Student { private String studentName; private int studentID; private String schoolName; public Student(){ this.studentName = "John Doe"; this.studentID = -1; this.schoolName = "N/A"; } public Student(String studentName, int studentID, String schoolName){ this.studentName = studentName; this.studentID = studentID; this.schoolName = schoolName; } public void setStudentName(String studentName){ this.studentName = studentName; } public void setStudentID(int studentID){ this.studentID = studentID; } public void setSchoolName(String schoolName){ this.schoolName = schoolName; } public String getStudentName(){ return this.studentName; } public int getStudentID(){ return this.studentID; } public String getSchoolName(){ return this.schoolName; } }
22nd Mar 2018, 4:22 PM
Fata1 Err0r
Fata1 Err0r - avatar
+ 3
@Devansh In all fairness, you said "please make changes in the code and post it," which is what I did for you. It's the class/object you were wanting to create, but done how one should do it. To understand how to properly utilize objects/classes, you need to understand the code that I posted to you in my other posts, because it addresses all of the issues you were having and all of the things you left out, from encapsulation to constructors to basic syntax. I know you want a quick, easy route, but the reality is that my "long code" that I wrote is the answer to everything you did wrong. To understand why is to read that long code and see it in action, which is why I put it in Code Playground for your observation. The code that I posted in the Playground is commented also. I removed commenting here because of limited characters in posts. Here is list that's causing error: - No main loop or its class. Program can't even start because there is no entry point into the program. - The only thing that your class `student` contains is a couple variables that you never use, which is contained inside of an incomplete default constructor. - Your class members (variables) should be set to private and you'll want to create getters/setters for each member. This is called encapsulation. This is how you'll manipulate your objects once instantiated, and how you'll also retrieve data from them. - Line 13-15: You're not concatenating your strings with your variables. - Line 13: getName() isn't a method you've created yet, so it can't be called. - Line 14: getRoll_no should be getRoll_no(). However, you never created that function, so it won't work anyways. - Line 15: getSchool should be getSchool(). However, you never created that function, so it won't work anyways. - Declare your members outside of constructor, make them private, and then set their initial values inside of the default constructor. Let me know if you need further help! I'll be happy to further explain if needed.
22nd Mar 2018, 6:28 PM
Fata1 Err0r
Fata1 Err0r - avatar
+ 2
hi Jakob thanks for you help but you wrote a long code while I wanted a simple explanation of whether what I wrote was right or not if not then why I just want to print student s1's details and what were the errors if you can explain the code is still in the description you can go through and help me out please thanks once again
22nd Mar 2018, 5:22 PM
Devansh Gupta
Devansh Gupta - avatar
+ 2
this is the modified version of what I wrote as the code https://code.sololearn.com/c1O9rHwRC1G1/?ref=app it is still without any getters and setters but I am pretty sure it should work but its not doing so please help me guys
23rd Mar 2018, 6:32 AM
Devansh Gupta
Devansh Gupta - avatar
+ 1
you didn't implement the getter methods you are using and you are missing the concatenation operator (+) between your string and value
22nd Mar 2018, 3:37 PM
Jeremy
Jeremy - avatar
+ 1
please make changes in the code and post it
22nd Mar 2018, 3:39 PM
Devansh Gupta
Devansh Gupta - avatar
+ 1
thanks Jakob you have been helping me since a few days back and now I find you at every question I post thanks Jakob again and I hope you Will keep helping me like this. but I still don't understand getters and setters and their use I am currently on Java's more on classes chapter and I am going a bit slow in order to understand typical concept like this (above mentioned) better thanks again ,keep up the work and do something to make me understand the concept if you can best regards/wishes
23rd Mar 2018, 2:21 PM
Devansh Gupta
Devansh Gupta - avatar