ReportPayment() method in main is giving errors - java

I'm asked to do the following tasks:
Five private variables to store the Name, Surname, Gender, age,
AmountPayout
Include an Object Instantiation that has two methods called. The one methods is called Info(), this will use a GUI to retrieve Name,
Surname, Gender, age, AmountPayout
Create another method called ReportPayment(), this will use a GUI to display the information of the user. Within this methods create
another method called PaymentCalculator that parse a parameter of the
amount to be paid.
Use the amount to deduct 15% tax, and return the final amount to be displayed.
My code returns:
required string string int double
public static void main(String[] args) {
Details det = new Details();
det.info();
det. ReportPayment();
}
class Details
{
private String name;
private String surname;
private String gender;
private int age , age1;
private double AmountPayout , SubPayout;
void info()
{
String name = JOptionPane.showInputDialog(null,"Enter the Patient Name :");
String surname = JOptionPane.showInputDialog(null,"Enter the Patient Surname :");
String age = JOptionPane.showInputDialog(null,"Enter the age of the patient :");
int age1 = Integer.parseInt(age);
String gender = JOptionPane.showInputDialog(null,"Enter the Patient gender :");
String AmountPayout = JOptionPane.showInputDialog(null,"Enter the Patient payout :");
double SubPayout = Double.parseDouble(AmountPayout);
}
void ReportPayment(String name, String surname, int age, double AmountPayout)
{
JOptionPane.showMessageDialog(null,"Victim of Listeriosis" + "\n"
+ "Patient Name:" + name + "" + surname + "\n"
+ "Age:" + age + "\n"
+ "Payout:" + AmountPayout);
}
}

First of all you should remove c# from tags, this has nothing to do with it.
Then take look at your ReportPayment method, as you can see in method signature you have 4 parameters that you must past once you call method.
So instead of writing det.ReportPayment();
You should write det.ReportPayment("name","surname", 10, 30); to make it work
But that isn't what you really want, because you are supposed to use class attributes you don't need to pass them to method, so you should change method to something like this:
// Note that we don't have parameters anymore
void reportPayment() {
JOptionPane.showMessageDialog(null,"Victim of Listeriosis" + "\n"
+ "Patient Name:" + name + "" + surname + "\n"
+ "Age:" + age + "\n"
+ "Payout:" + AmountPayout);
}
Last issue with your code is that you are not following conventions, this doesn't affect how your code works but it affects other people reading your code, whenever using some language make sure to get familiar with standard conventions in other to make code more understandable for other developers that will look at it.
In Java standard for naming is Camel case notation, with difference between class names and variable/method names being that classes always start with UPPERCASE letter while variable and method names start with lowercase.
Class name - StringBuilder
Method name - reportPayment
Variabl name - amountPayout
Another convention in Java is that { bracket comes just after method signature not on next line.
Finally in other to learn more about above mentioned things and many others I suggest you to buy/borrow some good Java introduction book, there are many out there.

2 issues with your code:
First issue: you forget the parameters in the call to det.reportPayment(); (note the lowercase r).
I assume you want to use the attributes stored in Details.
You have two options:
Pass the attributes as parameters:
det. ReportPayment(det.name, det.surname, det.age, det.AmountPayout);
Note this will not works without more modification as those attributes are privates. You have to either make them public or add getters (and then replace det.name with det.getName(), etc.)
Remove the parameters and use the Detail attribute directly in the method as the method is in the same class (best solution).
void reportPayment() {
JOptionPane.showMessageDialog(null, "Victim of Listeriosis" + "\n" + "Patient Name:" + this.name + "" + this.surname
+ "\n" + "Age:" + this.age + "\n" + "Payout:" + this.AmountPayout);
}
Second issue: you never set your class attribute.
In info()method, you ask the user to give values. Those values are stored in temporary variables but not in the class attributes.
For example, push the values in this.name instead of creating a new String name.
this.name = JOptionPane.showInputDialog(null, "Enter the Patient Name :");
this.surname = JOptionPane.showInputDialog(null, "Enter the Patient Surname :");
and so on.
Unrelated: have a look at java naming convention

Adding "this" in the info() method made the name , surname and gender work but the age and AmountPayout still returns a null
public static void main(String[] args) {
Details det = new Details();
det.info();
det.reportPayment();
}
}
class Details {
private String name;
private String surname;
private String gender;
private int age , age1;
private double AmountPayout , SubPayout;
void info()
{
this.name = JOptionPane.showInputDialog(null,"Enter the Patient Name :");
this.surname = JOptionPane.showInputDialog(null,"Enter the Patient Surname :");
String age = JOptionPane.showInputDialog(null,"Enter the age of the patient :");
int age1 = Integer.parseInt(age);
this.gender = JOptionPane.showInputDialog(null,"Enter the Patient gender :");
String AmountPayout = JOptionPane.showInputDialog(null,"Enter the Patient payout :");
double SubPayout = Double.parseDouble(AmountPayout);
}

Reworked the answer:
I tried to run the code myself and with a little tweaking it works perfectly fine. We are getting there. I think thi is the whole thing you should need!
This should now fix your error aswell as fit the requirements you have.
I changed:
renamed ReportPayment() to reportPayment()
removed te space inbetween
"det." and "ReportPayment() in the main class
Rewrote your attributes
rewrote the info() method
added the calculation for taxes as required
Main.java
public class Main {
private static boolean running = true;
public static void main(String[] args) {
Details det = new Details();
det.info();
det.reportPayment();
}
}
Details.java
import javax.swing.JOptionPane;
class Details {
private String name;
private String surname;
private int age;
private String gender;
private double AmountPayout;
void info() {
this.name = JOptionPane.showInputDialog(null, "Enter the Patient Name :");
this.surname = JOptionPane.showInputDialog(null, "Enter the Patient Surname :");
String rawAge = JOptionPane.showInputDialog(null, "Enter the age of the patient :");
this.age = Integer.parseInt(rawAge);
this.gender = JOptionPane.showInputDialog(null, "Enter the Patient gender :");
String rawPayout = JOptionPane.showInputDialog(null, "Enter the Patient payout :");
this.AmountPayout = Double.parseDouble(rawPayout);
}
void reportPayment() {
paymentCalculator();
JOptionPane.showMessageDialog(null, "Victim of Listeriosis" + "\n" + "Patient Name:" + name + " " + surname
+ "\n" + "Age:" + age + "\n" + "Payout:" + AmountPayout);
}
private void paymentCalculator() {
this.AmountPayout = this.AmountPayout * 0.85;
}
}

Related

How to call a value from another class with 3 java classes [duplicate]

This question already has answers here:
How do I print my Java object without getting "SomeType#2f92e0f4"?
(13 answers)
Closed 1 year ago.
I have 3 java class named Author, Book, and DisplayBook. Author class is for setting the name of the author. Book class is for geting details(title, author, price) of the book and DisplayBook is for displaying the output (title, author, price) in the console window.
This is what I have done so far but it displays a random text (Author#2f2c9b19) for the author. These are my codes with respective set and get methods.
Author class
private String firstName;
private String lastName;
public Author(String fname, String lname)
{
firstName = fname;
lastName = lname;
}
Book class
private String title;
private Author author;
private double price;
public Book(String bTitle, Author bAuthor, double bPrice)
{
title = bTitle;
author = bAuthor;
price = bPrice;
}
public void printBook()
{
System.out.print("Title: " + title + "\nAuthor: " + author + "\nPrice: " + price);
}
DisplayBook class
public static void main(String[] args) {
Author author = new Author("Jonathan", "Rod");
Book book = new Book("My First Book", author, 35.60);
book.printBook();
}
This is the output
How do I get Jonathan Rod to display beside Author: ?
Override the toString method of the Author class. Perhaps like so:
public String toString() {
return this.lastName +", "+ this.firstName;
}
The reason it is displaying Author#2f2c9b19 is because Java is displaying the memory address of that object as you are passing a whole object into the print.
your print book should look like this,
public void printBook()
{
System.out.print("Title: " + title + "\nAuthor Name: " + author.firstName + " " + author.lastName + \nPrice: " +
price);
}
whenever you just print any object , it calls toString method on that. Here you are NOT getting desirable output as its calling toString on Author which is not overrident in your class.
Please override toString method in Author class.

2nd JOptionPane won't come up

I'm just starting out with Java and programming in general. Could someone please explain to me why the second dialog box won't show up after I've entered the information for the first one?
Thanks!
// Java Practice
import javax.swing.JOptionPane;
import java.util.Scanner;
public class DialogTest
{
public static void main(String [] args)
{
Scanner keyboard = new Scanner(System.in);
String firstname;
String lastname;
int age;
JOptionPane.showInputDialog("What is " +
"your first name?");
firstname = keyboard.nextLine();
JOptionPane.showInputDialog("What is " +
"your last name?");
lastname = keyboard.nextLine();
JOptionPane.showInputDialog("How old are you?");
age = keyboard.nextInt();
JOptionPane.showMessageDialog(null, "I see, so your name is: " + firstname + lastname + " and you are" + age + " years old.");
System.exit(0);
}
}
JOptionPane.showInputDialog() returns a String that contains the value entered by the user. Instead of using the Scanner class, store the return value of the method call into your variables:
String firstname, lastname, age;
firstname = JOptionPane.showInputDialog("What is " +
"your first name?");
lastname = JOptionPane.showInputDialog("What is " +
"your last name?");
age = JOptionPane.showInputDialog("How old are you?");
JOptionPane.showMessageDialog(null, "I see, so your name is: " + firstname + lastname + " and you are" + age + " years old.");
You don't need both JOptionPane and Scanner. You only need one (I highly recommend Scanner over the other).
What's happening is this: The call to JOptionPane is opening a dialog for your user to enter a value. That value is returned by this method call, which you do nothing with. Then after the dialog is finished, you call keyboard.nextLine() which blocks the program until the user enters another value into the command line window (or your IDE if you're running it through that).
If you want to see both options available to you, try commenting out the keyboard lines and setting firstname = JOptionPane... and so on. Once you've tried out that program, do the opposite: comment out the JOptionPane calls and replace them with System.out.println calls.
As someone who began learning input handling via JOptionPane, I believe Scanner is a much better utility.

how to refer to strings from input dialog?

I need to prompt the user to enter their full name and once they do I need two separate messages to show them your first name is and your last name is. I have everything but what I need to code for the firstName and lastName string. I feel like it has something to do with indexOf? but I can't get it to work correctly.
public class project2b {
public static void main (String [] args) {
String firstName;
String lastName;
String fullName;
firstName =
lastName =
fullName = JOptionPane.showInputDialog(null, "What is your full name?");
JOptionPane.showMessageDialog(null, " Your first name is " +
firstName);
JOptionPane.showMessageDialog(null, " Your last name is " +
lastName);
}
}
String[] names = fullName.split ("\\s");
firstName = names[0];
lastName = names[1];
InputDialog will only return a single String. You need to parse it. String has a handy split() method that will do the parsing for you.
Assuming the user enters their first and last name separated by a space, this will work.
String fullName = JOptionPane.showInputDialog(null, "What is your full name?");
String[] names = fullName.split(" ");
String firstname = names[0];
String lastName = names[1];
My answer does not cover validation. You would normally validate the user's input before using it, but I believe it to be out of the scope of the question.

Cannot use a package in a program

I'll try to explain this as clearly as I can. I am trying to narrow down my code for an assignment by turning three of the classes used into a package that the program will access through an import statement. Here is my original code:
import javax.swing.JOptionPane;
public class AssignmentTen
{
public static void main (String[] args)
{
System.out.println();
int num = Integer.parseInt(args[0]);
int eNumber;
String input2;
String input3;
String input4;
String input5;
String input6;
int input7;
int input8;
int input9;
int input10;
Employee[] employees = new Employee[num];
for (int i = 0; i < num; i++)
{
eNumber = getInt ("Enter Employee Number:");
input2 = getString ("Enter Employee First Name:");
input3 = getString ("Enter Employee Last Name:");
input4 = getString ("Enter Employee Street:");
input5 = getString ("Enter Employee City:");
input6 = getString ("Enter Employee State (Initials):");
input7 = getInt ("Enter Employee Zip Code (5 Digits):");
input8 = getInt ("Enter Employee Hire Month (MM):");
input9 = getInt ("Enter Employee Hire Day (DD):");
input10 = getInt ("Enter Employee Hire Year(YYYY):");
Name name = new Name(input2, input3);
Address address = new Address (input4, input5, input6, input7);
Date hireDate = new Date (input8, input9, input10);
employees[i] = new Employee (eNumber, name, address, hireDate);
System.out.println("#" + employees[i].empNumber + "\n" + employees[i].empName + "\n" + employees[i].empAddress + "\nHire Date: " + employees[i].empHireDate + "\n\n");
}
}
public static int getInt(String paramString)
{
String str = JOptionPane.showInputDialog(paramString);
return Integer.parseInt(str);
}
public static String getString(String paramString)
{
String str = JOptionPane.showInputDialog(paramString);
return str;
}
}
class Employee
{
Number empNumber;
Name empName;
Address empAddress;
Date empHireDate;
public Employee(Number empNumber, Name empName, Address empAddress, Date empHireDate)
{
this.empNumber = empNumber;
this.empName = empName;
this.empAddress = empAddress;
this.empHireDate = empHireDate;
}
}
class Name
{
String firstName;
String lastName;
Name(String first, String last)
{
firstName = first;
lastName = last;
}
public String toString()
{
return firstName + " " + lastName;
}
}
class Address
{
String eStreet;
String eCity;
String eState;
int eZipCode;
Address(String street, String city, String state, int zipCode)
{
eStreet = street;
eCity = city;
eState = state;
eZipCode = zipCode;
}
public String toString()
{
return eStreet + " " + eCity + " " + eState + " " + eZipCode;
}
}
class Date
{
int month;
int day;
int year;
Date(int eMonth, int eDay, int eYear)
{
month = eMonth;
day = eDay;
year = eYear;
}
public String toString()
{
return month + "/" + day + "/" + year;
}
}
I need to take the Name class, the Address class, and the Date class and put them into a package called util. First of all, I organize all of my java programs in a folder called My Java Programs (C:\MyJavaPrograms) as I was instructed to do so. To create what I think is supposed to be the package, I copied each class and put them into their own individual file, I put the statement 'package.util' at the top, and then I named them Name.java, Address.java, and Date.java and I put them in a folder called util, which is located in C:\MyJavaPrograms.
Afterwards, I put the statement 'import util.*;' at the top of my original code and put it in C:\MyJavaPrograms. For one reason or another, the file won't compile. I've already done a lot of googling for my problem, including reading up on this, which doesn't help me, at least to the extent that I understand what is wrong.
I suspect that there is a problem with my classpath, although I do not have any idea what I can do to fix it. If it helps with figuring out the problem, I do know that I can't use the javac command in the command prompt without going to this thread and typing in one of the responses.
Ideally, I would want to compile my new assignment and have it do exactly what the code I posted at the top of this question would do now, except it does it while utilizing a pacakage with the extra classes. I would really appreciate it if someone could send me in the right direction.
Check your package definition (the first line in each .java file). It should be 'package util;' not 'package.util'.
Are the classes in your util package marked as public?
1) Breaking classes into separate files, and organizing the files into a package, is a Good Thing.
2) Suppose you wanted to break your classes into "Assignment10.java" and "Util.java". They could both be in package "com.myclass". You'd do the following:
a) Create the files Assignment10.java and Util.java
b) Put "package com.myclass;" at the top of both
c) Create a new directory "com\", and subdirectory "myclass\"
d) Copy both *.java files into com\myclass
e) Compile and run from your root directory
3) If you were using an IDE (like Eclipse or Netbeans), it will automatically create the directories for you
Here are a couple of good tutorials:
http://web.cs.wpi.edu/~cs2102/b12/Assignments/making-packages.html
Java Package Vs Folder-Structure? what is the difference
----------------------------- ADDENDUM -----------------------------
To compile and run a Java program with packages from the command line:
1) Create package folder:
mkdir myclass\util
2) Create .java files
cd myclass\util
notepad AssignmentTen.java =>
package myclass.util;
public class AssignmentTen {
public static void main (String[] args) {
System.out.println ("In main...");
Name name = new Name("Jack", "Torrance");
System.out.println ("Exiting main.");
}
}
notepad Name.java =>
package myclass.util;
public class Name {
public Name (String first, String last) {
System.out.println ("My name is " + first + " " + last + "...");
}
}
3) Compile
javac *.java
dir =>
10/04/2013 10:38 AM 589 AssignmentTen.class
10/04/2013 10:36 AM 248 AssignmentTen.java
10/04/2013 10:38 AM 593 Name.class
10/04/2013 10:35 AM 177 Name.java
4) Execute
cd ..\..
java myclass.util.AssignmentTen =>
In main...
My name is Jack Torrance...
Exiting main.

How to fix an arrayList?

So arrayLists are a first for me, and as far as I know I've been doing everything correctly and following the examples provided to me by my online course. HOWEVER, for some reason or other I have a line underlined red...which I will get to in a moment after a brief explanation of this program.
This program allows you to input an employee information and after pressing the 'list' button (listButton) it outsput in the employeeField etc etc. That basically sums up this program.
public class EmployeeView extends FrameView {
class Company { //this is the class to allow me to put 'company' in the arrayList...
String ID, firstName, lastName, annualSal, startDate, mileage;
Company (String _ID, String _firstName,String _lastName, String _annualSal, String _startDate) {
ID = _ID;
firstName = _firstName;
lastName = _lastName;
annualSal = _annualSal;
startDate = _startDate;
}
}
/** Define the ArrayList */
ArrayList <Company> inventory = new ArrayList <Company>();
private void AddActionPerformed(java.awt.event.ActionEvent evt) {
String c;
String ID, firstName, lastName, annualSal, startDate;
ID = IDField.getText(); //all this stuff grabs info from the Fields...which will then be stored in the array
firstName = firstNameField.getText();
lastName = lastNameField.getText();
annualSal = annualSalField.getText();
startDate = startDateField.getText();
The two lines below this is the culprit. I suppose "new" is't nessisary but it was there in the example so that's why I am using it...however when I get rid rid of it only 'company' is underlined and the 'c' in the 2nd line is underlined instead of having the entire line underlined. Anyways I hope this is making sense...since its (from what I know of) my only problem.
c = new Company(ID, firstName, lastName, annualSal, startDate);
inventory.add(c);
}
private void ListActionPerformed(java.awt.event.ActionEvent evt) {
String temp="";
for (int x=0; x<=inventory.size()-1; x++) {
temp = temp + inventory.get(x).ID + " "
+ inventory.get(x).firstName + " "
+ inventory.get(x).lastName + " "
+ inventory.get(x).annualSal + " "
+ inventory.get(x).startDate + "\n";
}
employeeTArea.setText(temp);
}
You've declared c to be a String; you can't assign a Company directly to a String.
Change your declaration of c to be Company.
c is declared as a String above. It should be type Company instead.

Categories

Resources