I try to run a programm but i get those four errors.
TestCusomer.java:25: error: toString() in Invoice cannot override toString() in Object (same thing at line 49)
and
line 59 cannot find symbol . myCustomer.setTrn(112233778)
line 60 cannot find symbol . myCustomer.setPersentage(150)
My programm is the following:
class Invoice
{
int trn; //TAX REGISTRATION NUMBER
int persentage;
public Invoice{}
public int setTrn(int trn){
this.trn = trn;
}
public int getTrn(){
return trn;
}
public void setPersentage(int persentage){
this.persentage = persentage;
}
public int getPersentage(){
return persentage;
}
String toString(){
System.out.println(trn+" : "+persentage);
}
}
class Customer{
int trn;
int charging= 0;
public Customer(int trn){
this.trn = trn;
}
public int charge(int amount){
charging = charging + amount;
}
public int charge(int amount , int trn){
if (this.trn == trn){
charging = charging + amount;
}
}
String toString(){
System.out.println(trn+" : "+charging);
}
}
class TestCustomer
{
public static void main(String[] args){
Customer myCustomer = new Customer(112233778);
myCustomer.charge(100);
myCustomer.setTrn(112233778);
myCustomer.setPersentage(150);
System.out.println(myCustomer);
}
}
few things,
You need to declare the toString method public
You need to return a String in your toString methods
I strongly suggest you add the #Override notation when you override a method to ensure that you actually override the method
The cannot find symbol... happen because those methods are not defined in Customer, you have those in Invoice
Your toString() methods need to return String objects. You are outputting a string in them but not returning a string. Also make them public.
For example, your toString() method for the Invoice class should be:
public String toString()
{
return trn + " : " + persentage;
}
And for your second problem (cannot find symbols), those methods are in the Invoice class and not in the Customer class so they cannot be called on a Customer object.
Related
I need to update a fields value inside one of my object, just update the String. The object is inside of an array. But I will target the object by typing in the objects "regNum" in the parameter.
This is what I tried, I really don't know how to use the set() method when I need to enter the list and the objects specific value.
public boolean doesNotWork( String regNumInput ){
for(int i = 0; i < meterList.size(); i++){
if(regNumInput == meterList.get(i).getRegNum()){
meterList.set(meterList.get(i).getWorkOrNot(), new String ("No"));
}
}
return true;
}
This Is the whole MeterArchive class that stores the meters and have some methods to it.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class MeterArchive
{
// instance variables - replace the example below with your own
ArrayList<Meter> meterList = new ArrayList<Meter>();
public void createClocks(){
Clock clockOne = new Clock("KH001", "Yes", "ClassRoom005", 0.0);
meterList.add(clockOne);
Clock clockTwo = new Clock("KH002", "Yes", "ClassRoom006", 0.0);
meterList.add(clockTwo);
}
public boolean doesNotWork( String regNumInput ){
for(int i = 0; i < meterList.size(); i++){
if(regNumInput == meterList.get(i).getRegNum()){
meterList.set(meterList.get(i).getWorkOrNot(), new String ("No"));
}
}
return true;
}
public void showAllMeter(){
for(Meter meter : meterList){
System.out.println(meter);
}
}
}
This is the clock class that has specific clock values that you can add.
public class Clock extends Meter
{
/**
* Constructor for objects of class Clock
*/
public Clock(String regNum, String workOrNot, String location, double minTime)
{
// initialise instance variables
super(regNum, workOrNot, location);
setMinTime(minTime);
}
//MINNIMUM TIME
public void setMinTime(double minTime){
this.minTime = minTime;
}
public double getMinTime(){
return minTime;
}
//EQUALS METHOD --- NOT SURE WHAT IT SHOULD DO... YET!
public boolean equals (Clock other){
return location.equals(other.location);
}
public String toString(){
String retur = super.toString() + "regNum: " + regNum +
"Does it work: " + workOrNot +
"Location: " + location +
"Min time value: " + minTime;
return retur;
}
}
This is the super class that has more general input for the different meters.
public class Meter
{
public String regNum;
public String workOrNot;
public String location;
/**
* Constructor for objects of class Clock
*/
public Meter(String regNum, String workOrNot, String location)
{
// initialise instance variables
setRegNum(regNum);
setWorkOrNot(workOrNot);
setLocation(location);
}
//REGISTRATION NUMBER
public void setRegNum(String regNum){
this.regNum = regNum;
}
public String getRegNum(){
return regNum;
}
//WORK OR NOT
public void setWorkOrNot(String workOrNot){
this.workOrNot = workOrNot;
}
public String getWorkOrNot(){
return workOrNot;
}
//LOCATION
public void setLocation(String location){
this.location = location;
}
public String getLocation(){
return location;
}
}
So in the MeterArchive class I want to change the field value "workOrNot" from whatever it is (most likely "Yes") to "No". I found out that set() is usually the way to go, but in this program I want the user to add the specific "regNum" and then the method will change to String inside the "workOrNot" field to "No". As I said earlier I dont know how to target the specific field inside the object. Can someone explain how to do this?
You need to use setter method setWorkOrNot() to update field workOrNot on the desired Meter object.
Use the below code:
public boolean doesNotWork( String regNumInput ){
for(int i = 0; i < meterList.size(); i++){
if(regNumInput.equals(meterList.get(i).getRegNum())){
meterList.get(i).setWorkOrNot("No");
}
}
return true;
}
I am new to java and learning basic concepts. I was learning abstraction and the most basic definitions i found was : Used to hide the complexity (hide how a process will be done and show what can we do?)
Fair enough. I got a basic idea of what is abstraction. But i am not clear in few things here:
Lets see the below example:
/* File name : Employee.java */
public abstract class Employee {
private String name;
private String address;
private int number;
public Employee(String name, String address, int number) {
System.out.println("Constructing an Employee");
this.name = name;
this.address = address;
this.number = number;
}
public double computePay() {
System.out.println("Inside Employee computePay");
return 0.0;
}
public void mailCheck() {
System.out.println("Mailing a check to " + this.name + " " + this.address);
}
public String toString() {
return name + " " + address + " " + number;
}
public String getName() {
return name;
}
public String getAddress() {
return address;
}
public void setAddress(String newAddress) {
address = newAddress;
}
public int getNumber() {
return number;
}
}
Salary.java
/* File name : Salary.java */
public class Salary extends Employee {
private double salary; // Annual salary
public Salary(String name, String address, int number, double salary) {
super(name, address, number);
setSalary(salary);
}
public void mailCheck() {
System.out.println("Within mailCheck of Salary class ");
System.out.println("Mailing check to " + getName() + " with salary " + salary);
}
public double getSalary() {
return salary;
}
public void setSalary(double newSalary) {
if(newSalary >= 0.0) {
salary = newSalary;
}
}
public double computePay() {
System.out.println("Computing salary pay for " + getName());
return salary/52;
}
}
Main.java
/* File name : AbstractDemo.java */
public class AbstractDemo {
public static void main(String [] args) {
Salary s = new Salary("Mohd Mohtashim", "Ambehta, UP", 3, 3600.00);
Employee e = new Salary("John Adams", "Boston, MA", 2, 2400.00);
System.out.println("Call mailCheck using Salary reference --");
s.mailCheck();
System.out.println("\n Call mailCheck using Employee reference--");
e.mailCheck();
}
}
My question here is , we cant even intsantiate an abstract class. So we have to extend it and overide the same method? When we override the abstract methos in the child class, the super class(abstract class method) is of no use. Also as we cant even intantiate , why cant we just write everything in one class instead of extending the abtsrcat class?
While extending the abstract class and overriding the same thing is it not a negative as the space will more for these waste abstract classes?
I know i dont have clarity and thats the reason i am confused. If anyone can clarify this (no stratight definions which are not useful for noobs like me) with explanation , i would really appreciate the time for that.
Aside of bad example you use, your understanding of Abstract class is not right:
So we have to extend it and overide the same method? When we override the abstract methos in the child class, the super class(abstract class method) is of no use
First: Abstract class and Interface both stay to provide abstract methods to be overridden by extending or implementing classes.
Second: Abstract class can have common methods implementation for all extending classes - then you do not need to implement or override them over and over again. Just use them. (Note: starting from Java 8 Interface also can have default implementation for methods)
Third: if you need another than common implementation - override needed method from super class.
Forth: if in your another implementation you need to run super method - do it at any time in the implementation by calling super.methodName(...)
Once upon modifying the above with static modifiers, line 16 requires the following syntax:
getLegs();toStrung();
//I think this is essentially printing the last called method updating class field variable toString. For example, to do setLegs();toStrung(); prints setLegs()'s toString.
Question: How should one access a shared field within methods? What if I included it into the constructor? Ideally, I want the code to look like getLegs().toStrung() and for toString to be a clean slate for every method.
My answer: I think a seperate instance of String toString inside each method works to get a clean slate appeal, but the syntax doesn't make sense. I know it is about my design. I think a solution would be a new class, but this returns to the same conflict that relates to the class field variable.
public class Dog{
public String toString;
public Dog(String name){
this.name = name;
}
public int getLegs(){
toString = "Dog has " + legs + " legs.";
return legs;
}
public int setLegs(int legs){
toString = getName() + "'s legs have changed from "
+ getLegs() + " to " + legs + ".";
this.legs = legs;
return this.legs;
}
public void toStrung(){
System.out.println(Dog.toString);
}
public static void main(String[] args){
Dog Dundt = new Dog("Dundt");
Dundt.getLegs();
Dundt.toStrung();
}
1) toString() should not be a static member.
2) getLegs() should not have the side effect of changing the member String toString.
3) There should not be a member variable String toString.
4) toString() should return a String.
5) name needs to be a member.
6) legs needs to be a member.
7) toString() should generate the string from the members at run time.
8) You do not need to explicitly call toString() in main. Simply passing the instance of Dog to println will call it for you.
9) It is good practice to annotate methods you are overriding with the #Override annotation. toString() is a member of Object and you are overriding Object.
public class Dog{
private String name;
private int legs = 4;
public Dog(String name){
this.name = name;
}
public int getLegs(){
return legs;
}
public int setLegs(int legs){
this.legs = legs;
return this.legs;
}
#Override
public String toString(){
return "Dog is called " + name + " it has " + legs + " legs.";
}
public static void main(String[] args){
Dog dundt = new Dog("Dundt");
System.out.println(dundt);
}
}
package book1;
import java.util.ArrayList;
public abstract class Book {
public String Book (String name, String ref_num, int owned_copies, int loaned_copies ){
return;
}
}
class Fiction extends Book{
public Fiction(String name, String ref_num, int owned_copies, String author) {
}
}
at the moment when i input values into the variable arguments and call them with this :
public static class BookTest {
public static void main(String[] args) {
ArrayList<Book> library = new ArrayList<Book>();
library.add(new Fiction("The Saga of An Aga","F001",3,"A.Stove"));
library.add(new Fiction("Dangerous Cliffs","F002",4,"Eileen Dover"));
for (Book b: library) System.out.println(b);
System.out.println();
}
}
i get a return value of this:
book1.Fiction#15db9742
book1.Fiction#6d06d69c
book1.NonFiction#7852e922
book1.ReferenceBook#4e25154f
how can i convert the classes to return a string value instead of the object value? I need to do this without changing BookTest class. I know i need to use to string to convert the values. but i don't know how to catch the return value with it. could someone please point me in the right direction on how to convert this output into a string value?
You need to overwrite the toString() Method of your Book class. In this class you can generate a String however you like. Example:
#Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(this.author).append(": ").append(this.title);
return sb.toString();
}
You need to override the toString() method in your Book or Fiction class. The method is actually declared in the Object class, which all classes inherit from.
#Override
public String toString(){
return ""; // Replace this String with the variables or String literals that you want to return and print.
}
This method is called by System.out.println() and System.out.print() when they receive an object in the parameter (as opposed to a primitive, such as int and float).
To reference the variables in the method, you'll need to declare them in the class and store them via the class's constructor.
For example:
public abstract class Book {
private String name;
private String reference;
private int ownedCopies;
private int loanedCopies;
public Book (String name, String reference, int ownedCopies, int loanedCopies) {
this.name = name;
this.reference = reference;
this.ownedCopies = ownedCopies;
this.loanedCopies = loanedCopies;
}
#Override
public String toString(){
return name + ", Ref:" + reference + ", OwnedCopies: " + ownedCopies + ", LoanedCopies: " + loanedCopies; // Replace this String with the variables or String literals that you want to return and print.
}
}
The classes you have defined, don't store any values. It is in other words useful to construct a new book. You need to provide fields:
public abstract class Book {
private String name;
private String ref_num;
private int owned_copies;
private int loaned_copies;
public String Book (String name, String ref_num, int owned_copies, int loaned_copies) {
this.name = name;
this.ref_num = ref_num;
this.owned_copies = owned_copies;
this.loaned_copies = loaned_copies;
}
public String getName () {
return name;
}
//other getters
}
Now an object is basically a set of fields. If you want to print something, you can access and print one of these fields, for instance:
for (Book b: library) System.out.println(b.getName());
In Java, you can also provide a default way to print an object by overriding the toString method:
#Override
public String toString () {
return ref_num+" "+name;
}
in the Book class.
Need to give your object Book a ToString() override.
http://www.javapractices.com/topic/TopicAction.do?Id=55
Example:
#Override public String toString()
{
return name;
}
Where name, is a string in the Class.
I am hoping that you have assigned the passed arguments to certain attributes of the classes. Now, once you are done with that, you can override the toString() method in Book to return your customized string for printing.
id appreciate anyones help here. Below is my class that contains all my setters and getters, in my main class, ive created 3 customers and in the value parameters, i have 3 different numbers. What i need to do is find the total value of all of those values, is there any way that i can create a method (See bookingValue below) that will calculate and add the the total of each customers value parameter? Bare in mind that 3 is not a fixed number, so the method should not be affected should i choose to add in more customers. This is probably really basic but if someone could get me on the right path, that'd be great, cheers
public class Customer
{
private int identity;
private String name;
private String address;
private double value;
public Customer()
{
identity = 0;
name = "";
address = "";
value = 0.0;
}
public void setIdentity(int identityParam)
{
identity = identityParam;
}
public int getIdentity()
{
return identity;
}
public void setName(String nameParam)
{
name = nameParam;
}
public String getName()
{
return name;
}
public void setAddress(String addressParam)
{
address = addressParam;
}
public String getAddress()
{
return address;
}
public void setValue(double valueParam)
{
value = valueParam;
}
public double getCarCost()
{
return value;
}
public void printCustomerDetails()
{
System.out.println("The identity of the customer is: " + identity);
System.out.println("The name of the customer is: " + name);
System.out.println("The address of the customer is: " + address);
System.out.println("The value of the customers car is: " + value + "\n");
}
public void bookingValue()
{
//Ive tried messing around with a for loop here but i cant seem to get it working
}
}
you can create an array of object of class customer and access the value in loop...
In the main function:
customer cus[]=new customer[num];
where num can be any number as 3 in your case
then get the "value" for each customer.. and then
public double bookingValue(customer []cus, int length)
{
double total=0.0;
for(int i=0;i<length;i++)
total+=a[i].value;
return total;
}'
return total value wherever you want to use.....
As in real life, one customer doesn't know anything about the other customers. If you would ask a customer in a store how much all customers spent, he will just look as confused as others reading this question.
I'd suggest implementing some CustomerManager, or Bookkeeper which hold all the customers internally (in a List for example). This CustomerManager needs to have methods to add and remove customers, the getBookingValue() method which loops over all customers in the CustomerManager's customers List and returns the total value and, if you please, some other comfort methods.
As an example:
public interface CustomerManager {
public void addCustomer(Customer customer);
public void removeCustomer(Customer customer);
public List<Customer> getCustomersByDate(long from, long to);
public double getBookingValue();
public double getBookingValue(List<Customer> customerList);
public List<Customer> getByAddress(String address);
public List<Customer> getByName(String name);
}