Hi! I`m new to java programming, and I recently got help from someone here on Ch
ID: 3670029 • Letter: H
Question
Hi! I`m new to java programming, and I recently got help from someone here on Chegg to write a program. (The program code is at the bottom of this post). (Link to see task if you`re interested: http://www.chegg.com/homework-help/questions-and-answers/need-help-java-programming-task-m-supposed-create-4-versions-program-ll-make-sure-give-thu-q10663835 ) The problem is, that when I paste this into java, I get several errors, (especially for "version 0.2"). I don`t know if the problem is due to errors in the code itself, or if I have done something wrong when creating the classes.
Here is what I did in Java (Eclipse):
-Create new java project
-Then I made a new class called Card (Sorry the picture is sideways)
-Then I pasted "version 0.1" into it. This one didn`t get any errors (from what I could see).
-Then I created a new class called Employee:
Then I pasted "version 0.2" into it. This is when the errors occur:
Then I created a interface and pasted "version 0.3 into it".
No errors here..
And then, at last, I created a class called Comparable (I know this must be wrong, but I don`t know how the chegg expert named the class "abstract class Card implements Comparable<Card>,Cloneable " considering that one can`t use blank spaces etc in the class name..
I`m not sure if the problems is in the java code itself, or am I doing something wrong when creating the classes? Below is the program codes if someone could please try it in their Java/Eclipse or similar, to see if it works, and tell me what`s wrong (rewrite the code if it`s wrong). I`ll make sure to give you a "thumbs up":-)
Version 0.1
public abstract class Card {
String firstName;
String lastName;
String pin;
long cardNumber;
boolean accessCode;
static long card = 1;
public Card() {
firstName = "Alex";
lastName = "Mitti";
pin ="1120";
cardNumber = card;
card++;
accessCode = false;
}
String getName(){
return firstName + " "+lastName;
}
abstract boolean checkPIN(int pin);
@Override
public String toString() {
return "[Name: "+firstName+" "+lastName+",PIN: "+pin+",Card Number: "+cardNumber+",IsCancelled: "+accessCode+"]";
}
boolean isCancelled(){
return accessCode;
}
}
Version 0.2
class Employee extends Card{
@Override
boolean checkPIN(int pin) {
int day, month, year;
int second, minute, hour;
GregorianCalendar date = new GregorianCalendar();
day = date.get(Calendar.DAY_OF_MONTH);
second = date.get(Calendar.SECOND);
minute = date.get(Calendar.MINUTE);
hour = date.get(Calendar.HOUR);
if(hour>=7 && hour<=17 && !isCancelled() && pin!=9999){
return true;
}
return false;
}
}
class Guest extends Card{
Date issueData;
public Guest(){
pin =9999;
issueData = new Date();
}
@Override
boolean checkPIN(int pin) {
Date currentDate = new Date();
int diffInDays = (int) ((currentDate.getTime() - issueData.getTime()) / (1000 * 60 * 60 * 24));
if(pin == 999 && diffInDays <=7 && (!isCancelled())){
return true;
}
return false;
}
}
version 0.3
import java.util.Calendar;
import java.util.GregorianCalendar;
public interface Constants {
void setFirstname(String firstName);
String getFirstname();
void setLastname(String lastName);
String getLastname();
void setFullName(String fullName);
String getFullName();
double computeCredit();
double computeBonus();
}
class Employee extends Card implements Constants{
double salaryPerHe;
GregorianCalendar issueData ;
public Employee() {
salaryPerHe =123.43;
issueData = new GregorianCalendar();
}
@Override
boolean checkPIN(int pin) {
int day, month, year;
int second, minute, hour;
GregorianCalendar date = new GregorianCalendar();
year = date.get(Calendar.YEAR);
day = date.get(Calendar.DAY_OF_MONTH);
second = date.get(Calendar.SECOND);
minute = date.get(Calendar.MINUTE);
hour = date.get(Calendar.HOUR);
if(hour>=7 && hour<=17 && !isCancelled() && pin!=9999){
return true;
}
return false;
}
@Override
public void setFirstname(String firstName) {
this.firstName= firstName;
}
@Override
public String getFirstname() {
return firstName;
}
@Override
public void setLastname(String lastName) {
this.lastName =lastName;
}
@Override
public String getLastname() {
return lastName;
}
@Override
public void setFullName(String fullName) {
String names[] = fullName.split("\s+");
firstName = names[0];
lastName=names[1];
}
@Override
public String getFullName() {
return firstName+" "+lastName;
}
@Override
public double computeCredit() {
return salaryPerHe*45; // constant 45
}
@Override
public double computeBonus() {
GregorianCalendar date = new GregorianCalendar();
int currentYear = date.get(Calendar.YEAR);
int startYear = issueData.get(Calendar.YEAR);
return (currentYear-startYear)*45; // 45 constant
}
}
version 0.4
abstract class Card implements Comparable<Card>,Cloneable {
String firstName;
String lastName;
int pin;
long cardNumber;
boolean accessCode;
static long card = 1;
public Card() {
firstName = "Alex";
lastName = "Mitti";
pin = 0;
cardNumber = card;
card++;
accessCode = false;
}
String getName(){
return firstName + " "+lastName;
}
abstract boolean checkPIN(int pin);
@Override
public String toString() {
return "[Name: "+firstName+" "+lastName+",PIN: "+pin+",Card Number: "+cardNumber+",IsCancelled: "+accessCode+"]";
}
boolean isCancelled(){
return accessCode;
}
@Override
public int compareTo(Card o) {
if(this.lastName.compareTo(o.firstName) !=0)
return -1;
return this.firstName.compareTo(o.firstName);
}
}
Explanation / Answer
Hi, program is correct.
1. Create a project
2. create a package or else you can create in 'src' folde of your newly created progect
3. put each classes in different .java file. File name should be same as Corresponding java class name.
4. If you get error --- red color type icon --- the cleck on read color icon..it will show what is problem. You may have to import some in-built classes of java
For example.
In Employee.java
import java.util.*;
Basically in each .java file ... copy above given statement (BOLD color), and paste after class name
Ex.
import java.util.*;
class Employee{
}
like that in each .java file
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.