Simple Project use of classes, copy constructor and aggregation JAVA by Ravinder Nath Rajotiya - April 20, 20210 A simple project Following Classes: 1. Student Class 2. Subject Class 3. ExamPaper Class Aggregation example: when you have field in one class that are also the object in another class, there comes a concept what is called aggregation. We can access those object members from this class object using aggregation concept. See code in main method for aggregation: System.out.println(paper.getSubject().getSubjectCode()); //similarily if you want to extract only the student number, see how you can System.out.println("Student number:"+paper.getStudent().getStudentNumber()); Let us see the complete project involving all the above classes in intellij package com.example; public class classAndObjects { public static void main(String[] args) { Subject subject1 = new Subject ("ETEE 310", "MP&MC"); Student std1=new Student("Ravinder", 1234); Exampaper paper = new Exampaper(std1, subject1, 100); //System.out.println(paper); //we can extract more information from paper object, say we want to know the subject code. //See the
Copy Constructor in JAVA JAVA by Ravinder Nath Rajotiya - April 19, 2021May 10, 20210 Copy Constructor in JAVA A copy constructor i a constructor that creates a copy of an existing object of the same class. It creates an object using another object of the same Java class. That's helpful when we want to copy a complex object that has several fields, or when we want to make a deep copy of an existing object. Example copy complex number, subject detail. Here are example code in JAVA
Classes and Objects in JAVA JAVA by Ravinder Nath Rajotiya - April 18, 2021April 18, 20210 Classes and Objects in JAVA Everything that we see around us are objects for example people, metals, car, tree, bike, animals and so on. Every object belong to certain class. So, we have to understand their classification to differentiate among them. Class is way of describing some properities, functionality or behaviour of an objects. It is a template or blueprint for an object. For example a house class will have four properties- windows, doors, roof, walls. so we can create the following properties of one house and their number or type may differ from hour=se to house. House Object-1 House Object-2 House Object-3 Property Numbers or type Property Numbers or type Property Numbers or type Windows 20 Windows 10 Windows 8 Doors 4 Doors 3 Doors 2 Type of roof Tiles Type of roof Grass Type of roof Wood Walls Bricks with plaster Walls Bricks Walls plaster As seen in above table all house have same
String Arrays in JAVA JAVA by Ravinder Nath Rajotiya - April 18, 20210 String arrays Steps: Step-1: Declare String object and call the methode getNames() Step-2: Use for loop to print Step-3: Inside for loop, check if the string is null Step-4: If string not null, then pring names usring Sysem.out.println(names[i]) Here after main() will be the method definition for getNames() Example-1: package com.example; public class arrays { public static void main(String[] args) { String [] names = getNames(); for(int i=0; i<names.length;i++) { if (names[i] != null) { System.out.println(names[i]; } } } /** * method to input String array * Unlike an array where by default all element get initialized to '0' * In an String array by default all the elements are initialized to null * @return names */ public static String[] getNames() { String[] names = new String[5]; names[0] = "Abhay"; names[1] = "Banwari Lal"; names[2] = "Chanchal"; names[3] = "devender"; names[4]
ArrayList Class in JAVA JAVA by Ravinder Nath Rajotiya - April 18, 20210 ArrayList class in JAVA We have already seen that arrays are of a fixed length and after they are created, they cannot grow or shrink. This meant the the programmer must know in advance about the size requirement of an array. ArrayList like arrays also allow object storage, but unlike array, the arrayList are created with an initial size, the arrayList collection is automatically enlarged when object are added and it automatically get shrunk when objects are removed from the arrayList. There is a class ArrayList to which is in java.util.* package. You always work with ArrayList Object which you pass it to it as: ArrayList<Integer> // Use class names as Integer, remember you cannot use primitive data type as int ArrayList<String> names = new