public class Course { private static int nextId=1200; private int id; private String name; private int credits; public Course(){ id=nextId++; } public Course(String name ,int credits){ id=nextId; this.credits=credits; this.name=name; } public int getId(){ return id; } public String getName(){ return name; } public void setName(String name){ this.name=name; } public int getcredits(){ return credits; } public void setCredits(int credits){ this.credits=credits; } public static void setnextId(int nextId){ Course.nextId=nextId; } public static int getNextID(){ return nextId; } public String toString(){ return "Course ID:"+id+ "\nCourse NAME:" +name+ "Credits:"+credits; } } public class CourseDemo { public static void main(String[] args) { Course c1=new Course(); c1.setName("OOPJ"); c1.setCredits(3); System.out.println(c1); Course c2=new Course("COSM", 3); System.out.println(c2); } }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter