Hi, I've just started a Java course, which is giving me trouble because I've had so much unscheduled overtime. I have three programs due that have small problems that I can't fix no matter how many changes I've made. They are really basic, but I've only been one week into this. I've marked each instance. There isn't many. Any help will be appreciated. Thanks, Joe

Number 1:

import java.util.Scanner;  //needed for scanner class

public class DiscussionOne 
{
	public static void main(String[] args) 
	{
//		Added by Michael Jenkins
//		Constructor for all non-static methods in this class file.  
//		dissOne can be called for any other methods added to this class.
//		No need to add another constructor.
//		Page 139 discusses constructors
		DiscussionOne dissOne = new DiscussionOne();
//		Example call to method displaying a personal greeting
		dissOne.displayWelcomeMessage();};
//		End of code by Michael Jenkins
	

		/**
		 * Added by Michael Jenkins
		 * This method asks user for input (name)
		 * Then displays a personalized welcome
		 * message to the user.
		 */
	public void displayWelcomeMessage() {
		//	Scanner class used to get input from user.
		//	Pages 86-89
	Scanner userInput = new Scanner(System.in);
//	print and println are used to send output to the screen.
//	Pages 37-42
	
	System.out.print("Please type your full name: ");
	String name = userInput.nextLine();
	System.out.print("Welcome, " + name);
	System.out.println(", to the first Discussion Question.");
	}
	//added by Joe Jaunich
	// using Constructors starting fron page 139
		
	
			double percentmarkedup;       // Product's markeup from regular price
			double originalprice;   		// Product's original price
			double salestaxrate;         // Product's sales tax rate

			// these parameters were adapted from the sales illustration on p.52			
// Joe added line to get keyboard input p. 86	
	
    Scanner keyboard = new Scanner(System.in);	//used to get input from keyboard 

//	 Get the starting Price.
    
    System.out.print("What is your product's "  //this is one problem
                     + "starting price? ");                    //according to Eclipse
    
    originalprice = keyboard.nextDouble();
    
    // Get the monthly markup rate.
    System.out.print("What is your purchase markup rate? ");
    percentmarkedup = keyboard.nextDouble();
    
     // Get the amount of pay for the month.
    System.out.print("How much were you paid this month? ");
    salestaxrate = keyboard.nextDouble();
    
// used the following to calculate and display the final sale price
    //combination of pp.60-65, and the layout of code listing 2-10 on p. 51
 
    	double storeSalPrice = originalprice*percentmarkedup/100;	// figures markup
    	
    	double tempSalPrice = originalprice+storeSalPrice;			// adjusts for markup
    	
    	double tempTaxPrice = tempSalPrice*salestaxrate/100;		// figures sales tax
    	
    	double taxSalPrice = tempSalPrice+salestaxrate;				// adds sales tax
    	
    	double finalPrice = tempSalPrice+salestaxrate;				// final price
    	
    	System.out.println("The original price="+originalprice);	// the following prints out
    	
    	System.out.println("The markedup percentage="+percentmarkedup);	//findings
    	
    	System.out.println("The store Selling price="+tempSalPrice);
    	
    	System.out.println("The sales tax rate="+ salestaxrate);
    	
    	System.out.println("The sales tax price="+ tempTaxPrice);
    	
    	System.out.println("The final Price="+ finalPrice);
    	}
 
    	}

Number 2:

public class EmployeeClass {

	/**
	 * First set the Employees fields
	 */
	
	public static void main(String[] args)
	{
		String Name = new String (employee);		//holds employee name
		String Place = new String (department);		//holds employee department
		String Job = new String (position);			//holds employee position
		int idnumber;								//holds employee id number
		
		   	/**
		   	 * The setemployee method is a mutator that accepts an argument
		   	 * an argument which is stored in the employee field.
		   	 */

		     public void setEmployee; 
		     {
		    	 employee = emp;
		     }
		     
		    /**
			 * The setdepartment method is a mutator that accepts an argument
			 * an argument which is stored in the department field.
			 */

			 public void setDepartment;
			 {
				 department = dep;
			 }

			/**
			 * The setposition method is a mutator that accepts an argument
			 * an argument which is stored in the position field.
			 */

			 public void setPosition;
			 {
				 position = pos;
			 }

			/**
			 * The setidnumber method is a mutator that accepts an argument
			 * an argument which is stored in the idnumber field.
			 */

			 public void setIdnumber;
			 {
				 idnumber = id;
			 }

			/**
			 * The getemployee method is a accessor that 
			 * returns the value stored in the employee field.
			 */

			 public void getEmployee;
			 {
			     return employee;
			 }

			/**
			 * The getdepartment method is a accessor that 
			 * returns the value stored in the department field.
			 */

			 public void getDepartment;
			 {
			     return department;
			 }

			/**
			 * The getposition method is a accessor that 
			 * returns the value stored in the position field.
			 */

			public void getPosition;
			{
			     return position;
			}

			/**
			 * The getidnumber method is a accessor that 
			 * returns the value stored in the idnumber field.
			 */

			public int getIdnumber();     //this is another problem
			{
			     return idnumber;
			}
							     
}

}

Number 3:
import javax.swing.JOptionPane;
	
public class EmployeeDataOut {

	
	/** Designed to use Employee Class methods: employee,
	 * department, position, and idnumber
	 */ 
	public static void main(String[] args) {
		
		// Create three employee objects
		
			String Name = "Susan Meyers";		//holds employee name
			String Name = "Mark Jones";			//holds employee name
			String Name = "Joy Rodgers";		//holds employee name
		    String name,  						// Our input's name
	        inputString;  						// Holds input
			
		   	/**
		   	 * The individual Employees data
		   	 * is stored in the employee fields below.
		   	 */
			
			public void EmployeeData;("Susan Meyers"); //this is another problem
		     {
		    	 employee = "Susan Meyers";
		    	 department = Accounting;
		    	 position = "Vice President";
		    	 idnumber = 47899;
		     }
			public void EmployeeData;("Mark Jones"); //this is another problem
			 {
			   	 employee = "Mark Jones";
			   	 department = IT;
			   	 position = Programmer;
			   	 idnumber = 39119;
			 }
			public void EmployeeData;("Joy Rodgers"); //this is another problem
			 {
			   	 employee = "Joy Rodgers";
			   	 department = Manufacturing;
			   	 position = Engineer;
			   	 idnumber = 81774;
			 }
					
	   // Get the user's name.
	   name = JOptionPane.showInputDialog("What is your name?");
		
	   
	   
	   // After this line the results will be displayed.
	   JOptionPane.showMessageDialog(null, "Hello " + name +
	                     ". Your company data is ");
	   // I'd like to call up the object definition, but I don't know the command.
	   if (name = "Susan Meyers")
		   JOptionPane.showMessageDialog(null, +   
			   	"employee = Susan Meyers"  +
			   	"department = Accounting"  +
				"position = Vice President"  +
				"idnumber = 47899");
		   
		if (name = "Mark Jones")   
			   JOptionPane.showMessageDialog(null, +
				"employee = Mark Jones" +
			   	"department = IT" +
			   	"position = Programmer" +
			   	"idnumber = 39119");
			   
		if (name = "Mark Jones")
			JOptionPane.showMessageDialog(null, +
			   	"employee = Joy Rodgers" +
			   	"department = Manufacturing" +
			   	"position = Engineer" +
			   	"idnumber = 81774");
	   
	   // End the program.
	   System.exit(0);			 				
		
		
	}

}

Recommended Answers

All 18 Replies

This is not how it works, that you just say I have programs, have problems with them as they do not work properly and just drop code. Not to mention that you did not use code tags [code=Java]CODE HERE [/code]

State what sort of errors you getting, or what the program should do, or what is expected output and what you getting. Maybe then we will considerate look at it...

Adding to what peter_budo has said, I would like to suggest you to refer the Java Code Conventions document. Any beginner needs this, in your case since I do not see any indentations in your code which can make the code unreadable, I site this to you.

Also I see a lot of comments in your code, I do not know whether you have put them for us to understand or for yourself, in any ways I would like to tell you that a majority of the comments that I have seen prove to be unmeaningful.

import java.util.Scanner; //needed for scanner class

Comments are a very essential part of any programming langauge, they help in mentioning the programmer's "intended" logic for a method/code block/etc to others. But comments such as these are unrequired for, since anyone claiming to be a java programmer has to know why a particular package/class is needed.
Also else where I see you commenting the end of class etc., here too such comments could have been avoided, since a programmer is well supposed to know how and where a class or method ends. An exception to this rule is writing the end of for and while loops, wherever these loops run long and end up one after the another, since in such cases the programmer might not be able to figure out which brace ends what loop. (Modern IDEs that allow brace checking at the click of a button remove out this exception too)
The reason I am pressing so much on comments is, that a habit of writing too many comments would eventually make you search for your code in your comments, when writing long complex programs.

> String Name = new String (employee); Strings in Java are immutable, you don't need to use new String() , ever.

> if (name = "Mark Jones") I think you should try reading the basic Java tutorials hosted by Sun and do a bit of practice.

String Name = new String (employee);	
String Place = new String (department);	
String Job = new String (position);

As it so happens, and as ~s.o.s~ points it out, this is not only a not a good way to create Strings in java, it is the worst way of doing it.
As a matter of fact, you would find this in most of the how=not-to-code essays for java strings.
To put a stamp of authority on this, this is exactly what Joshua Bloch mentions in Effective Java, Item 5 : Avoid creating unnecessary objects.
It creates a new String object unnecessarily everytime.
String objects should be created like this :

String myUsername = "verruckt24";

Adding to what peter_budo has said, I would like to suggest you to refer the Java Code Conventions document. Any beginner needs this, in your case since I do not see any indentations in your code which can make the code unreadable, I site this to you.

Also I see a lot of comments in your code, I do not know whether you have put them for us to understand or for yourself, in any ways I would like to tell you that a majority of the comments that I have seen prove to be unmeaningful.


Comments are a very essential part of any programming langauge, they help in mentioning the programmer's "intended" logic for a method/code block/etc to others. But comments such as these are unrequired for, since anyone claiming to be a java programmer has to know why a particular package/class is needed.
Also else where I see you commenting the end of class etc., here too such comments could have been avoided, since a programmer is well supposed to know how and where a class or method ends. An exception to this rule is writing the end of for and while loops, wherever these loops run long and end up one after the another, since in such cases the programmer might not be able to figure out which brace ends what loop. (Modern IDEs that allow brace checking at the click of a button remove out this exception too)
The reason I am pressing so much on comments is, that a habit of writing too many comments would eventually make you search for your code in your comments, when writing long complex programs.

Thanks, I'll get into using comments ASAP, I've just been crunched with time with early, large requirements from my first Java class. You're point is well taken, I'll follow up.

Thanks, I'll get into using comments ASAP, I've just been crunched with time with early, large requirements from my first Java class. You're point is well taken, I'll follow up.

Good you plan to take up my point. But rather my point was, that do not put the habit of excessive commenting, comments should be written only when there is some complex logic to be explained to other readers/maintainers of the program.

String Name = new String (employee);	
String Place = new String (department);	
String Job = new String (position);

As it so happens, and as ~s.o.s~ points it out, this is not only a not a good way to create Strings in java, it is the worst way of doing it.
As a matter of fact, you would find this in most of the how=not-to-code essays for java strings.
To put a stamp of authority on this, this is exactly what Joshua Bloch mentions in Effective Java, Item 5 : Avoid creating unnecessary objects.
It creates a new String object unnecessarily everytime.
String objects should be created like this :

String myUsername = "verruckt24";

Yes when using my Java coursebook, I used in my object definition from it. After I saw your comment, I went back to it and it later used your method. I'll change those string creations right away. I guess it should look something like this:
String myUsername = "employee";
String myDepartmentname = "department";
String myPositionname = "position";

I'm bringing up Ecllipse and putting this in as I type. As you can tell, I am reading a basic Java test, I guess it's taking this blockhead a while to get it straight.

You're input have been very useful. If you see any other issues, please let me know

If you are a beginner in the Java language, as I guess you are, I would suggest you not to use any modern IDEs for some while. What these modern IDEs do to you is that they give away to you all the things that an amateur programmer shouldn't be given at all. They complete the code for you, they present you the javadocs with just a F2 press, they compile the code for you, they make JARs for you, they set the environment for you, they indent the code for you, they put in matching braces for you etc etc., all the things that a beginner should rather NOT be given.
Whenever learning a new language, in this case Java, it's not enough to just learn the syntax, you should learn the complete process from setting up of the java development toolkit to being able to run and compile JARs on command line. You should know what is the classpath, why to use it, how to use, how to set it. You should know how to set up the offline java docs, where to access them online, how to search within the docs for a particular package/class/method rather than the IDE presenting you the exact concise part of the docs, you should be able to keep in mind atleast the most common of the packages/classes/interfaces/methods etc. All this will only make you understand, grasp, absorb more of the langauge and it's working environment.

After a while when you feel that you are comfortable enough doing all this things, and you need not spend your time repeatedly doing them by hand, then you can switch over to these IDEs. Till then one should practice avoiding temptation and keep doing things on his own.

Disclaimer : Things expressed here are exclusively IMO. ;)

Yes, I've fixwd that EmployeeClass file with fields like private String myUsername;. I'm now trying to do the EmployeeDataOut file. Thanks, for the suggestion. In EmployeeDataOut I'm trying to figure out how to combine those fields with the employee. I'm stuck at the moment.

If you set some variables as private and need to access them from some other class you need to provide getter and setter methods

public void setEmployeeName(String name){
    employeeName = name;}

public String getEmployeeName(){
    return employeeName;}

If you set some variables as private and need to access them from some other class you need to provide getter and setter methods

public void setEmployeeName(String name){
    employeeName = name;}

public String getEmployeeName(){
    return employeeName;}

Peter, good stuff. Here's how I did it :
ec1.setIdnumber(47899);
ec1.setMyDepartmentname("Accounting");
ec1.setmyPositionname("Vice President");
Then:
getIdnumber() + ec1.getMyDepartmentname() + ec1.setmyPositionname());

I'm still having trouble compiling because of the last parameter, which doesn't make sence to me!

Thanks much for your input.

yup. Its also standard convention to use "CamelCase" notation for variable and procedure names. Methods should ideally be verbs e.g acessor methods (getPrice or something) or modifier methods (setPrice or something). Its standard to begin class names with a capital letter but not to do so for anything else (thats why int has no capital and String is a capital - thats because String is a class not a primitive datatype.

@jbennet :
You mention "CamelCase" as I supposed too, till about an year ago, when I came across the actual camel case which has the first letter of every word capitalized. Now Java, we knew certainly had the first letter of the very first word in lower case. So I just decided to go through the Java Code Conventions and here's what they say about method names - "Methods should be verbs, in mixed case with the first letter lowercase, with the first letter of each internal word capitalized.". Rather they do not mention the CamelCase word anywhere in their document about "Naming Conventions"

i said that.

This line is not complete getIdnumber() + ec1.getMyDepartmentname() + ec1.setmyPositionname()); Can you post whole statement and include error you getting from it?

PS: I think you two should google Hungarian notation to settle your case

PS: I think you two should google Hungarian notation to settle your case

No, I don't think so. Because, even though Hungarian notation follows the first letter in lower case rule with all internal words' first letter capitalized, it is quite different in the sense that in this notation the name of the variable is supposed to give it's type too. So for e.g. an integer variable might be named iValue a String variable might be named strUsername etc. Here the first word/letter specifies the type, with the second word often called the given name.

Don't you think that since 1976 when Charles Simonyi wrote his PhD Thesis there was development/changes specially after the author become employee of Microsoft?

Didn't get the point here. Are you hinting at a modified version of the notation ?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.