Posts

Example of time format using Constructor Overloading

Image
Let us first see our roti class. public class roti {     int hour, minute, second;          public roti() {         setTime(0,0,0);     }     public roti(int h) {         setTime(h,0,0);     }     public roti(int h, int m) {         setTime(h,m,0);     }          public roti(int h, int m, int s) {         setTime(h,m,s);     }          public void setTime(int h, int m , int s) {         setHour(h);         setMinute(m);         setSecond(s);     }          public void setHour(int h) {         hour = ((h>=0 &...

toString

Image
Whenever we are using the constructor to initalise the value without using any method we need to use toString method to print the value of the parametes. In this example we shall see what will happen if we don't use  toString() and then when we use it. A. Without toString() CODE OF ROTI CLASS public class roti {      private int age;      private String name;           roti (int age, String name){          this.age = age;          this.name= name;      } } CODE OF SABZI CLASS public class sabzi {          public static void main (String args[]) {                     roti rotiObject = new roti(4, "Dheeraj"); System.out.println(rotiObject);         } } OUTPUT WHEN USING toString public class roti {  ...

Get GirlName using setName and getName

Image
 This code will require two classes. One is papaya and the other one mango. public class papaya {     private String girlName;          papaya(){         setName("MJ");     }          public void setName(String girlName) {         this.girlName = girlName;     }          public String getName() {         return this.girlName;     }          public void printDetails() {         System.out.printf("The name of your girl is %s", getName());     }      } Now let us see class mango which is our main class. class mango {     public static void main (String[] args) {              papaya papayaObject = new p...