Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It only takes a minute to sign up.
Student management system project in java with source code.
Is there a better way to implement my code in Java? But in Java, it is different, as it won't let me input data on the string array name. It seems to skip and move on to ask the user for the age rather than starting on name.
Here is how I would code this. This was implemented independently from Msnik's answer which is great by the way. The interesting part is in main and the use of Scanner. This solution also uses an ArrayList to store the results, and a for-each loop to iterate through them. We want to call nextLine to collect an entire line of input at a time.
You saw it skip over name and go straight to age for a similar reason - the call to nextInt captured your menu choice after the user hits enter but it did not capture the line return itself - the subsequent call to nextLine did that consuming both calls on the menuchoice and denying the user the chance to enter a name. Instead we'll call nextLine every time. For our string values we can assign this directly.
For our int s we'll use parseInt to convert first. We must catch a NumberFormatException in the event the user fails to enter an intand in this case we'll just prompt them again. Say you were given a student. What properties might it have? For example, it may have a name, a gender, a major, a list of courses, etc etc.
What could you ask it to do? You could ask it to tell you its name, to tell you whether it is enrolled in a given course or not, whether it shares a course with a given other student or not, etc etc. In your example, Students appear to have a name, an age, a course, a year, and a section.
These are the values that define a student. We can begin a more OOP approach by defining a Student class that adequately stores this information about a student, and allows construction and access to these fields:. Now, you may notice that everything in the above example is public. This is almost certainly wrong. For example, making the age field public allows anyone to walk up to a student and say "you are now years old".Rajesh in section A got 45 marks.
Suresh in section B got 78 marks. Ramesh in section A got 83 marks. Kamlesh in section A got 77 marks. Vignesh in section B got 93 marks. Here we have a created an array of 5 Students. Since Student is class i. That is why five Student objects are created and the variables namemarks and section are initialized separately.
As usual, accessing of the variables of student objects can be done using the dot.
Subscribe to RSS
Open In App. Try Free Demo in about 11 hours. Join Live Class. Create Effort Sprints. Topics Index. Why We Need Programming 1. Runtime Math Entry Interface TreeMap Stack Class Dictionary Properties Class Calendar Class To create an array list in Java, you declare an ArrayList variable and call the ArrayList constructor to instantiate an ArrayList object and assign it to the variable:.
Note that the capacity is not a fixed limit.Learning Java: Part 14: Using ArrayLists
You can use the generics feature to specify the type of elements the array list is allowed to contain:. If you specified a type when you created the array list, the objects you add via the add method must be of the correct type. You can insert an object at a specific position in the list by listing the position in the add method:.
After these statements execute, the nums array list contains the following strings:. If you use the add method to insert an element at a specific index position and there is not already an object at that position, the add method throws the unchecked exception IndexOutOfBoundsException.
To Print Student Details Using Classes In Java
To access a specific element in an array list, use the get method and specify the index value beginning with zero of the element that you want to retrieve:. You can also use an enhanced for statement, which lets you retrieve the elements without bothering with indexes or the get method:. Here, each String element in the nums array list is printed to the console. To determine the index number of a particular object in an array list when you have a reference to the object, use the indexOf method:.
Here, an enhanced for loop prints the index number of each string along with the string. Use the set method to replace an existing object with another object within an array list. For example:.
Here, an array list is created with a single string whose value is One. Then, the value of the first element is replaced with the value Uno. To remove a specific element based on the index number, use the remove method:. The removeRange method removes more than one element from an array list based on the starting and ending index numbers.
This method removes all elements between the elements you specify, but not the elements you specify. You can also use the removeAll method to remove all the objects in one collection from another collection.
A similar method, retainAllremoves all the objects that are not in another collection. Doug Lowe began writing programming books before Java was invented. Use Array Lists in Java.Standard arrays in Java are fixed in the number of elements they can have. If you want to increase of decrease the elements in an array then you have to make a new array with the correct number of elements from the contents of the original array.
An alternative is to use the ArrayList class. The ArrayList class provides the means to make dynamic arrays i. An ArrayList can be created using the simple constructor :. This will create an ArrayList with an initial capacity for ten elements. If a larger or smaller ArrayList is required the initial capacity can be passed to the constructor.
To make space for twenty elements:. Note: The ArrayList only stores objects so although the above lines appear to add int values to ArrayList the are automatically changed to Integer objects as they are appended to the ArrayList.
A standard array can be used to populate an ArrayList by converted it to a List collection using the Arrays. One thing to note about ArrayList is the elements don't have to be of the same object type. Even though the dynamicStringArray has been populated by String objects, it still can accept number values:. To minimize the chance of errors it's best to specify the type of objects you want the ArrayList to contain. This can be done at the creation stage by using generics:.
Now the if we try to add an object that isn't a String a compile-time error will be produced. An object can be inserted anywhere into the ArrayList index of elements by using the add method and passing the position for the insertion.
To add the String "Max" to the dynamicStringArray at position The remove method can be used to remove elements from the ArrayList. This can be done in two ways. The first is to supply the index position of the element to be removed:. The second is to supply the object to be removed. This will remove the first instance of the object. To remove "Max" from the dynamicStringArray :. Rather than removing an element and inserting a new one in its place the set method can be used to replace an element in one go.
Just pass the index of the element to be replaced and the object to replace it with. To replace "Peter" with "Paul":.
Share Flipboard Email. Paul Leahy. Computer Science Expert. Paul Leahy is a computer programmer with over a decade of experience working in the IT industry, as both an in-house and vendor-based developer.
Use the add method to append a value to the ArrayList :. To display the items in an ArrayList the toString method can be used:. The String "Max" is no longer in the ArrayList :. There are a number of useful methods to help navigate the contents of an arraylist:.
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It only takes a minute to sign up. I am just looking for code review and overall critique. I want to know what I can do better and how can I do things more efficiently in Java. I need particular feedback on the use of my ArrayList in my colsToRows method and then how I converted it back to regular arrays in Java.
What better step could I have taken there? The better step could be to just stick with either arrays or ArrayListif that's feasible. Converting between them is adding complexity to your code. Your max and min methods are perfectly idiomatic, although you may want to check that the array passed into them has. Defining a sum method could help reduce code duplicated between mean and meanLowDrop. You may also want to handle the case of empty arrays or of meanLowDrop receiving an array of size And as one last point, moving most of main into a separate method that looks something like this:.
While I agree with BenC that the mean and meanLowDrop methods need some protection against empty array parameters, this check should be at a higher level, for example in this method:. Sign up to join this community. The best answers are voted up and rise to the top. Home Questions Tags Users Unanswered.
Student grades program Ask Question. Asked 4 years, 3 months ago. Active 4 years, 3 months ago. Viewed 3k times. ArrayList; import java. Clever Programmer Clever Programmer 7 7 silver badges 13 13 bronze badges.
But I don't know of any actual benefit apart from it being more readable to me. What you are saying makes sense. By the way for my grade letter method Should I put the if and else if statements logic one line or leave it as is? Which is more idiomatic in Java? It's almost never a good idea to put condition and code block on one line like that.
Active Oldest Votes. BenC BenC 2, 6 6 silver badges 21 21 bronze badges. Like my mean or my lowest grade dropped function? Then how do I avoid the code complexity as you mentioned of converting from list double to double ? If everything else is doublethen colsToRows returning a List is the only thing adding complexity.
This makes a lot of sense! Transposed method returning a regular double was exactly what I needed in order to reduce the code complexity. I have also added the printStats method now. I really appreciate this, this is awesome! In the same method, another check is due: if studentScores.
Sign up or log in Sign up using Google.It can be shrinked or expanded based on size. ArrayList is a part of collection framework and is present in java. An ArrayList:. E here represents an object datatype e.
The Integer class wraps a value of the primitive type int in an object. An object of type Integer contains a single field whose type is int. This data is provided by a custom inner class which is formed by combination of various primitive object datatypes.
Consider a case when we have to take input as N number of students and details are: roll number, name, marks, phone number A normal method using object arraylist would be:. Now using a custom Arraylist: The custom ArrayList simply supports multiple data in the way as shown in this image. To construct Custom ArrayList. This article is contributed by Shubham Saxena. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.
See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. Writing code in comment? Please use ide. String name. Data int roll, String name, int marks, long phone.
Basically, in case 5, I would like the user to attach a student from the students array onto a module from the modules array without duplication. First, your Student and Module class only need some identical minor tweaks. They're functionally identical, so I'm only going to detail changes to the Student class. This will allow you to store references to the exact object, not just its name. Also, you should generally code to using an interfacenot a classas this will make certain implementation changes easier.
Wrap all returns of your module list in this case, getModulesso it can't be modified this will prevent some inadverdant changes :. Look into immutable objects, although I don't necessarily recommend it for you just yet - it requires some different thinking. Add an equals method and maybe hashcode as well.
Eclipse or any other good IDE should be able to generate it for you, although only use name for the comparisons ignore modules this time around. You're going to want a small utility method for adding modules to students and students to modules in place of all the instances where they're manually written. The interface is:. The actual implementation is left to the reader. You may want to check first to verify that they don't have the cross-references already Your getMainMenu isn't actually getting anything it returns null - change the return type to voidand rename it to printMainMenu.
You should avoid the use of 'magic numbers' like the plague. The only numbers which should be used without creating constants and even this is context sensitive are -1, 0, 1, 2 and sometimes In several places in your code, you mix normal for loops with foreach loops.