A comparison between Java and C++ for GUIWhen it comes to the development of a Graphical User Interface, one of the aspects the programmer should put into consideration is making the interface as user-friendly as possible. This will ensure that any person who uses the application for whom the GUI is...
Text File Reading
/**
* A beehive will consist of only one Queen bee (is not considered as a Bee
* object as it is quite different). Other entities include eggs, larva, pupa,
* female worker bees and male drone bees. The population of worker and drone
* bees fluctuate depending on the varying daily egg laying of capability of the
* queen. With the exception of the Queen, all eggs, larva, pupa, worker and
* drones are considered as Bee objects to be instantiated from the Bee class.
* For future extension purposes, the instances of the class Bee should be made
* sortable based on the age attribute.
*
*/
public class Bee {
public static int beeCount = 0; // Whole number – (Class Variable/shared across all instances)
private int beeId; // Whole number (Unique and incremental identifier for each instance)
private int age; // Whole number
private String type; // Text (values include 'egg','larva', 'pupa', 'worker','drone')
private int nectarCollectionSorties; // Whole number (how many times a worker bee has gone to fetch nectar on a
// particular day)
private boolean eaten; // True/False (whether the larva/worker/drone received its share of honey on a
// particular day)
private boolean alive; // True/False (whether or not the larva/worker/drone is alive)
public Bee() {
beeCount += 1;
this.beeId = beeCount;
this.age = 0;
this.type = "Egg";
this.nectarCollectionSorties = 0;
this.eaten = false;
this.alive = true;
}
public Bee(int age, String type) {
beeCount += 1;
this.beeId = beeCount;
this.age = age;
this.type = type;
this.nectarCollectionSorties = 0;
this.eaten = false;
this.alive = true;
}
public int compareTo(Object obj) {
if (obj instanceof Bee) {
Bee bee = (Bee)(obj);
if(bee.getBeeId() < this.beeId) {
return -1;
} else if (bee.getBeeId() > this.beeId) {
return 1;
} else {
if (bee.getType().equals(this.type)) {
return 0;
}
}
}
return 1;
}
public int getAge() {
return age;
}
public int getBeeId() {
return beeId;
}
public int getNectarCollectionSorties() {
return nectarCollectionSorties;
}
public String getType() {
return type;
}
public void incrementAge() {
this.age += 1;
}
public void incrementNectarCollectionSorties() {
this.nectarCollectionSorties += 1;
}
public boolean isAlive() {
return this.alive == true;
}
public boolean isEaten() {
return this.eaten == true;
}
public void setAge(int age) {
this.age = age;
}
public void setAlive(boolean alive) {
this.alive = alive;
}
public void setEaten(boolean eaten) {
this.eaten = eaten;
}
public void setNectarCollectionSorties(int nectar) {
this.nectarCollectionSorties = nectar;
}
public void setType(String type) {
this.type = type;
}
@Override
public String toString() {
// return "Bee ID: " + this.beeId +
// "\nAge: " + this.age +
// "\nType: " + this.type +
// "\nNectar Collection Sorties: " + this.nectarCollectionSorties +
// "\nHas eaten: " + this.eaten +
// "\nIs alive: " + this.alive;
return "Bee [beeCount=" + beeCount + ", beeId=" + beeId + ", type=" + type + ", age=" + age
+ ", nectarCollectionSorties=" + nectarCollectionSorties + ", eaten=" + eaten + ", alive=" + alive
+ "]";
}
}
Java classes and objects
import java.util.Scanner;
public class ChocolateCoupons {
// Entry point of the program
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// Ask the money of the user
System.out.println("How much money can you spend on chocolate bars?");
int cash = in.nextInt();
// Calculate how many chocolate bars can be bought
// Each choc costs $1
int numChocs = cash;
// Each choc has a coupon
int numCoupons = numChocs;
// We use the coupons to buy another set of chos
// 6 coupons is to 1 choc
while (numCoupons>= 6) {
numCoupons -= 6;
numChocs += 1;
// More coupon for each choc
numCoupons++;
}
// Report the total number of chocs
System.out.println("After redeeming coupons, you have "
+ numCoupons + " leftover coupons and can purchase a total of "
+ numChocs + " chocolate bars.");
}
}