PDA

View Full Version : Low-Level Java Help


C.J.
03-16-2006, 08:16 PM
I'm having a problem coding a program for class, if you have an understanding of Java, please give me an IM at CJ108706, I should be on all day.

D3adcell
03-16-2006, 08:56 PM
Could you list the problem you are having, and then possibly even post your code? I havent done java in quiet a while. Dropped the course too, but it's pretty similiar to c++.

IEatFriedPikmin
03-16-2006, 09:04 PM
i learned basic java in highschool 2 years ago. i will probably be rusty on expressing the exact code, but i could help you out. I am also taking c++ right now, and they are very similar.

C.J.
03-16-2006, 11:24 PM
//This class defines an Employee

//This class views an employee from the "inside". This class is an
// ADT class that DEFINES a single employee

//What does this class know about?
// The internal workings of an employee from basic information about each
// employee to how methods are actually implemented.

//What does this class NOT know about?
// How an employee will be used by other classes


import java.util.Scanner;

public class Employee {

//Static variable: this is a single variable shared by all objects
private static Scanner scan = new Scanner(System.in);

//Instance variables: each employee has its own copy of this information

private int idNum;
private String lastName;
private double hourlyWage;
private double weeklyPay;

//Instance methods

//This method will read in values for most of the instance variables
public void setFieldValues() {
System.out.print("ID number: ");
idNum = scan.nextInt();
System.out.print("Enter last name: ");
lastName = scan.next();
System.out.print("Enter hourly wage: $");
hourlyWage = scan.nextDouble();
}

//This method will compute the weekly pay based on a 40-hour work week
public void calculateWeeklyPay() {
weeklyPay = hourlyWage * 40;
}


//This method displays the salary information about an employee
public void printFieldValues() {
System.out.println("ID number: " + idNum +
"\nName: " + lastName +
"\nPay per hour: $" + hourlyWage +
"\nPay per week: $" + weeklyPay);
}


//This method prompts for and reads a new hourly wage.
public void readNewHourlyWage() {
System.out.print("Enter new hourly wage: ");
}
}

That is the employee class.

This is the DriveEmployee class.
//This client program is a driver program to test out the Employee class

//This class views an employee from the "outside". This class is an
// application class that USES one or more employees

//What does this class know about?
// It wants to work with two employees: a boss and a clerk
// It knows the names of the methods that will allow it to input vales
// for an employee, to calculate the weekly salary for an employee, and
// to display salary information about an employee.

//What does this class NOT know about?
// What information is stored about each employee
// How the methods carry out their tasks.

public class DriveEmployee {

public static void main(String[] args) {

//Variables that can refer to Employee objects
Employee aBoss;
Employee aClerk;

//Instantiating 2 new Employee objects
aBoss = new Employee();
aClerk = new Employee();

//Prompt and read information about the 2 employees
System.out.println("Enter information about a boss.");
aBoss.setFieldValues();
System.out.println();
System.out.println("Enter information about a clerk.");
aClerk.setFieldValues();

//Calculate the weekly pay for both employees
aBoss.calculateWeeklyPay();
aClerk.calculateWeeklyPay();

//Display information about both employees
System.out.println();
System.out.println("Salary information about a boss.");
aBoss.printFieldValues();
System.out.println();
System.out.println("Salary information about a clerk.");
aClerk.printFieldValues();
}
}


The instructions she gave us are as follows:

Modify the Employee class as follows:

add a new method:
public void readNewHourlyWage()
that prompts for and reads a new hourly wage.

Modify the DriveEmployee class as follows:

at the end of the main method, add code to test the new method above by reading in a new hourly wage for the boss, recomputing the boss's salary, and displaying the boss's information again.

WolfmanNCSU
03-17-2006, 03:12 AM
Wolf to the rescue. I think we figured this one out guys.

C.J.
03-17-2006, 03:18 AM
THIS JUST IN:

Wolfy is the greatest.

IEatFriedPikmin
03-17-2006, 06:27 AM
good to hear.

C.J.
04-03-2006, 02:05 AM
Update for more stupid.

DriveBankAccount

//This driver program tests out the BankAccount class

public class DriveBankAccount {

public static void main(String[] args) {

double stephensBalance;

BankAccount stephensAcct = new BankAccount (123, "Jones", 1000.00);

BankAccount colesAcct = new BankAccount ();

System.out.println(stephensAcct.toString()); //.toString() is optional
System.out.println(colesAcct);

colesAcct.deposit(150.00);

System.out.println(colesAcct.getBalance());

stephensAcct.withdraw(300.00);

stephensBalance = stephensAcct.getBalance();
System.out.println(stephensBalance);

System.out.println(stephensAcct);
System.out.println(colesAcct);

/*
stephensAcct.setAccountNumber(999);

System.out.println(stephensAcct.getAccountNumber() );

System.out.println(stephensAcct);

colesAcct.setAccountNumber(456);

System.out.println(colesAcct.getAccountNumber());

System.out.println(colesAcct);
*/
}
}


BankAccount

public class BankAccount {

//Instance variables

private int accountNumber;
private String name;
private double balance;

//Constructors

public BankAccount() {
name = "dummy";
}

public BankAccount(int accountNumberIn,
String nameIn,
double balanceIn) {
accountNumber = accountNumberIn ;
name = nameIn ;
balance = balanceIn ;
}

//Instance methods

public void deposit (double amount) {

balance = balance + amount;
}

public void withdraw (double amount) {

balance = balance - amount;
}

public double getBalance () {

return balance;
}

public String toString () {

return "Account number: " + accountNumber +
"\tName: " + name +
"\tBalance: " + balance;
}

public void setAccountNumber() {

}
}

WolfmanNCSU
04-03-2006, 02:42 AM
Wolf is on the scene again....

-Edit-
And we are successful

Dralor
04-03-2006, 03:37 AM
damn you work quick how come you where not that much help with my C++?:-p

C.J.
04-03-2006, 05:09 AM
As it turned out, my mistake was my wonderful inability to check my code for typos.

D3adcell
04-03-2006, 05:27 AM
typos are always a problem, miss spelling a variable or forgetting a ; or =

Just one wrong and it will mess up the entire program.

Backlash
04-03-2006, 05:37 AM
That's why I stuck with being a n00b programmer and only program in VB.NET.

Dralor
04-03-2006, 06:45 AM
What is real fun is when you add an Ostream and forget to put in into the perameters when calling the function. The compiler totally freaks out then.

Nexus
04-03-2006, 12:03 PM
Damn I could have helped you with this one. Oh well maybe next time.

WolfmanNCSU
04-03-2006, 06:08 PM
damn you work quick how come you where not that much help with my C++?:-p

I have forgotten what the problem was with C++

But next time, I guess I owe you one.

Dralor
04-03-2006, 08:44 PM
Ahh no biggie I usually fighure them out on my own but some of them were good fun.

Teh Roxor!
04-03-2006, 10:16 PM
Damn, I should have gone here for help when I was trying to make a level-order traversal of a binary tree. I figured it all out eventually, but it took a while.

cliffbo
04-03-2006, 10:54 PM
its all too tech for me lol

WolfmanNCSU
04-03-2006, 10:55 PM
Ewwww, I remember that assignment......

We had to make our own BTree class, sort it, traverse it, etc. I hated it then, but now that stuff makes great sense.....how sad is that.

Dralor
04-07-2006, 12:58 AM
Ohh coding a C++ file to convert text emails into html is annoying. Expecially when you have so search for keywords and turn them red as well.