You are on page 1of 5

Java Classes (Learning from C++) without this

#include <iostream>
using namespace std;
class Animal{
float age;
public:
Animal(){
setAge(0.0f);
}
Animal(float a){
setAge(a);
}
float getAge(){
return age;
}
void setAge(float a){
if(a > 0.0f){
age = a;
}else{
age = 0.1f;
}
}
};

int main(){
Animal objAnimal;
cout << "Age of the animal is " << objAnimal.getAge() << endl;
return 0;
}

Changing it to Java
class Animal{
private float age;
public Animal(){
setAge(0.0f);
}
public Animal(float a){
setAge(a);
}
public float getAge(){
return age;
}
public void setAge(float a){
if(a > 0.0f){
age = a;
}else{
age = 0.1f;
}
}
}
class Test{
public static void main(String args[]){
Animal objAnimal = new Animal();
System.out.println("Age of the animal is " + objAnimal.getAge());
}
}

Java Classes (Learning from C++) with this


#include <iostream>
using namespace std;
class Animal{
float age;
public:
Animal(){
setAge(0.0f);
}
Animal(float age){
setAge(age);
}
float getAge(){
return age;
}
void setAge(float age){
if(age > 0.0f){
this->age = age;
}else{
this->age = 0.1f;
}
}
};

int main(){
Animal objAnimal(2.5f);
cout << "Age of the animal is " << objAnimal.getAge() << endl;
return 0;
}

Changing it to Java
class Animal{
private float age;
public Animal(){
setAge(0.0f);
}
public Animal(float age){
setAge(age);
}
public float getAge(){
return age;
}
public void setAge(float age){
if(age > 0.0f){
this.age = age;
}else{
this.age = 0.1f;
}
}
}
class Test{
public static void main(String args[]){
Animal objAnimal = new Animal(2.5f);
System.out.println("Age of the animal is " + objAnimal.getAge());
}
}

Java GUI with FlowLayout

import javax.swing.JOptionPane;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JFrame;
import java.awt.FlowLayout;
class Milk{
private int quantity;
public Milk(){
setQuantity(0);
}
public Milk(int quantity){
setQuantity(quantity);
}
public void setQuantity(int quantity){
if(quantity>0){
this.quantity=quantity;
}else{
this.quantity=1;
}
}
public int getQuantity(){
return quantity;
}
public String getDetails(){
String message= "Quantity of Milk is:" + getQuantity();
return message;
}
}
class Fruit{
private String name;
private int quantity;

public Fruit(){
setName("undefined");
setQuantity(0);
}
public Fruit(String name){
setName(name);
setQuantity(0);
}
public Fruit(String name,int quantity){
setName(name);
setQuantity(quantity);
}
public void setName(String name){
if(name!=""){
this.name=name;
}else{
this.name="undefined";
}
}
public void setQuantity(int quantity)
{
if(quantity>0){
this.quantity=quantity;
}else{
this.quantity=1;
}
}
public String getName(){
return name;
}
public int getQuantity(){
return quantity;
}
public String getDetails(){
String message="Fruit name is:" + getName() + "Fruit quantity:" +
getQuantity();
return message;
}
}
class MilkShake{
private Milk objMilk;
private Fruit objFruit;

public MilkShake(){
objMilk=new Milk();
objFruit=new Fruit();
}
public MilkShake(int quantityofMilk){
objMilk=new Milk(quantityofMilk);
objFruit=new Fruit();
}
public MilkShake(int quantityofMilk,String name){
objMilk=new Milk(quantityofMilk);
objFruit=new Fruit(name);
}
public MilkShake(int quantityofMilk,String name,int quantityofFruit){
objMilk=new Milk(quantityofMilk);
objFruit=new Fruit(name,quantityofFruit);
}
public int getFruitQuantity(){
return objFruit.getQuantity();
}
public int getMilkQuantity(){
return objMilk.getQuantity();
}
public String getFruitName(){
return objFruit.getName();
}

public String getDetails(){


String message = "Milk Details are" + "Milk quantity" +
objMilk.getQuantity() + "Fruit Details are" + "Fruit name is:" + objFruit.getName()
+ "fruit quanity" + objFruit.getQuantity();
return message;
}
public double calculatePrice(double PriceofMilk,double PriceofFruit){
return ((objMilk.getQuantity()*PriceofMilk) +
(PriceofFruit*objFruit.getQuantity()));
}
}
class MyFrame{
private JFrame objFrame;
private JLabel objLabel2;
private JLabel objLabel3;
private JLabel objLabel4;
public MyFrame(String name, MilkShake objMilkShake){
objFrame=new JFrame(name);
objFrame.setVisible(true);
objFrame.setSize(500,500);
objFrame.setLayout(new FlowLayout());
objLabel2=new JLabel("Fruit Name: " + objMilkShake.getFruitName());
objLabel3=new JLabel("Fruit Quantity: "+objMilkShake.getFruitQuantity());
objLabel4=new JLabel("Milk Quantity: "+objMilkShake.getMilkQuantity());

objFrame.add(objLabel2);
objFrame.add(objLabel3);
objFrame.add(objLabel4);
}
}
class Test{
public static void main(String args[]){
MilkShake objMilkShake=new MilkShake(10,"Apple",30);
MyFrame objMyFrame = new MyFrame("Milk Shake Details", objMilkShake);
}
}

You might also like