Smipple is shutting down Nov 30, 2017. Some features are currently disabled.
Profile image for phoenix studio phoenixstudio
date - 5.3.2012
Language
Java
Tags
class java object

Intro to class and object

1 class Student{ 2 3 private String name; 4 private double cpa; 5 6 public Student(){ //consructor 7 8 name = ""; 9 cpa=0.0; 10 11 } 12 13 public Student(String n,double d){ 14 15 name=n; 16 cpa=d; 17 18 } 19 20 21 public void setName(String n){ //use void,no return value 22 23 name=n; 24 25 } 26 27 public void setCpa(double d){ //no return value 28 29 cpa=d; 30 31 } 32 33 public String getName(){ 34 35 return name; 36 37 } 38 39 public double getCpa(){ 40 41 return cpa; 42 43 } 44 45 public void display(){ 46 47 // System.out.printf("student %s has CPA %.2f\n",name,cpa); // %f=display (double) %.2f= display double, 2 dp 48 System.out.println("student named "+name+" has CPA "+cpa); 49 50 } 51 52 53 54 } 55 56 public class classIntro{ 57 public static void main (String[] args){ 58 59 Student a = new Student(); 60 a.setName("kambing");//method1 to declare and display 61 a.setCpa(3.999); 62 a.display(); 63 64 System.out.printf("New student named %s has CPA %.2f\n",a.getName(),a.getCpa()); //method3 to declare and display 65 66 Student b = new Student("ahmad",3.50);//method2 to declare and display 67 b.display(); 68 69 70 71 } 72 }

Comments