Java getter setter method [duplicate] - java

This question already has answers here:
Set and Get Methods in java?
(16 answers)
Closed 3 years ago.
I am making a program to calculate the cost of carpet fitting. I am struggling to write a setter getter message for the following variables... labourCharge, price and postCode.

This is how you should write your code. Assuming that your class is CarpetCostEstimator. The constructor takes the three values of labourCharge, postCode and price and sets them as follows:
public CarpetCostEstimator(double labourCharge, String postCode, double
price)
{
this.price = price;
this.postCode = postCode;
this.labourCharge = labourCharge;
}
public double getLabourCharge()
{
return this.labourCharge ;
}
public void setLabourCharge(double labourCharge){
this.labourCharge = labourCharge
}
In the above code, I've showed you how to put a setter and getter for labourCharge, you can do the same for the other properties of price and postCode.

You need to create a getter/setter inside the class only
public class CarpetCostEstimator {
double price = -1.0;
String postCode = "XXX-XXX";
double labourCharge = 1.0;
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getPostCode() {
return postCode;
}
public void setPostCode(String postCode) {
this.postCode = postCode;
}
public double getLabourCharge() {
return labourCharge;
}
public void setLabourCharge(double labourCharge) {
this.labourCharge = labourCharge;
}
}

In intellij idea it is convenient to generate getter and setter by means of a combination alt+insert, at the expense of other studios I do not know

Related

Bunch of errors and IDK why [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed last year.
Improve this question
I am a new coder, and have no clue why im getting a bunch of errors in this program im trying to make? can someone show me where I went wrong? I think im having issues with figuring out constructors and classes. I think my methods are correctly formatted, but I may be wrong.
public class Bank
{
public static void main(String[] args)
{
private double balance;
private String name;
private int pin;
public void Bank(double balance, String name, int pin)
{
this.balance = balance;
this.name = name;
this.pin = pin;
}
public String withdraw(double amount)
{
if(balance - amount >= 0)
{
double val = (double)(balance - amount);
String value = String.valueOf(val);
return value;
}
else
{
return " insufficient funds";
}
}
public String getName()
{
return name;
}
public int getPin()
{
return pin;
}
//commented out
//System.out.println("Succesfully excecuted. ");
}
}
You are keeping your instance variables, constructor and other method inside the main method. You need to keep them outside the main block but inside the class. You can call a method from another method but you cannot declare a method inside another method.
public class Bank
{
private double balance;
private String name;
private int pin;
public void Bank(double balance, String name, int pin)
{
this.balance = balance;
this.name = name;
this.pin = pin;
}
public static void main(String[] args)
{
// create object and perform operation
//commented out
//System.out.println("Succesfully excecuted. ");
}
public String withdraw(double amount)
{
if(balance - amount >= 0)
{
double val = (double)(balance - amount);
String value = String.valueOf(val);
return value;
}
else
{
return " insufficient funds";
}
}
public String getName()
{
return name;
}
public int getPin()
{
return pin;
}
}
Variable with modifiers are defined at class level not a method level. Paste this code outside the main method and you're good to go.
Also this is my first answer here :)

first year CS exam, need guidense [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
i have my exams tomorrow in java. It is focus on encapsulation, cohesion, coupling, inheritance and interface. I made a project of seven classes, speaker and headset class inherits from sound class and product class and DjMixer implements just from product.The main class is still empty. I need to know if I'm making any crucial mistakes in my code design.
public interface Product
{
double getPrice();
void setPrice(double price);
String getId();
void setId(String Id);
String getPlace();
void setPlace(String place);
}
public abstract class Sound
{
public abstract void setDesibel(int desibel);
public abstract int getDesibel();
public void amp() {
System.out.println("Recomended amplifier effect is 40-250 watts");
}
}
public class Speaker extends Sound implements Product
{
double price;
String id;
String place;
int desibel;
/**Price in Norwegain Kroner */
public double getPrice(){
return price;
}
/**Price is set in Norwegain Kroner */
public void setPrice(double price){
this.price = price;
}
/**Location of the product*/
public String getPlace(){
return place;
}
public void setPlace(String place){
this.place = place;
}
/**The products European Article Number*/
public String getId(){
return id;
}
/**type in the European Article Number under the barcode*/
public void setId(String serialNum){
id = serialNum;
}
//Constructor
public Speaker(double price, String location, String serialNum) {
super.amp();
this.price = price;
place = location;
id = serialNum;
}
public int getDesibel() {
return this.desibel;
}
public void setDesibel(int desibel) {
this.desibel = desibel;
}
}
public class Headset extends Sound implements Product
{
double price;
String place;
String id;
int desibel;
boolean wireless = true;
/**Price is in Norwegain Kroner */
public double getPrice(){
return price;
}
/**Price is set in Norwegain Kroner */
public void setPrice(double price){
this.price = price;
}
/**Location of the product*/
public String getPlace(){
return place;
}
/**Set location where item is stored */
public void setPlace(String location){
place = location;
}
/**The products European Article Number*/
public String getId(){
return id;
}
/**type in the European Article Number under the barcode*/
public void setId(String serialNum){
id = serialNum;
}
//Constructor
public Headset(double price, String location, String serialNum) {
super();
this.price = price;
place = location;
id = serialNum;
}
public void setDesibel(int desibel) {
this.desibel = desibel;
}
public int getDesibel() {
return this.desibel;
}
}
public class DjMixer implements Product
{
double price;
String id;
String place = "Storage room, shelf 3";
/**Price in Norwegain Kroner */
public double getPrice(){
return price;
}
/**Price is set in Norwegain Kroner */
public void setPrice(double price){
this.price = price;
}
/**Mixing tables is stored on shelf 3, if moved please set new place */
public String getPlace(){
return place;
}
/**Set new place for mixing table if moved */
public void setPlace(String location){
place = location;
}
/**The products European Article Number*/
public String getId(){
return id;
}
/**type in the European Article Number under the barcode*/
public void setId(String serialNum){
id = serialNum;
}
public DjMixer(double price, String serialNum){
this.price = price;
id = serialNum;
}
}
It would be a good idea to declare any variable that can be accessed via a "get" method as private. Also, you put "desibel" instead of "decibel" in some of your methods. If your teacher has taught you the final keyword, the wireless boolean seems like it should be declared as such.

Pass values to parameters

when i trying to do this i got the problem said
Constructor Product in class Product cannot be applied to given types;
required: java.lang.String,int,double; found: java.lang.String;
reason: actual and formal arguments lists differ in length
And i have 2 classes:
import java.text.*
public class Product {
private String name;
private int stock;
private double price;
public Product(String name, int stock, double price) {
this.name = name;
this.stock = stock;
this.price = price;
}
public double sell(int n) {
stock = n;
return stock;
}
public void restock(int n) {
}
#Override
public String toString() {
return stock + name + "at $"+price;
}
}
public class Store {
public static void main(String[] args) {
new Store().use();
}
private Product product;
private Product cashRegister;
public Store() {
product = new Product("Sticky tape");
cashRegister = new Product("Cash register");
}
public void use() {
}
private void sell() {
}
private void restock() {
}
private void viewStock() {
}
private void viewCash() {
}
private void help() {
System.out.println("Menu options");
System.out.println("s = sell");
System.out.println("r = restock");
System.out.println("v = view stock");
System.out.println("c = view cash");
System.out.println("x = exit");
}
}
I understand that i have to declare for Product constructor. But i think i have done it. If anyone know where i got wrong please explain. Thank you!
you do not have constructor with one param, so you can not using this form
product = new Product("Sticky tape");
decare one more constructor with one param or fill all param
product = new Product("Sticky tape", 10, 20.0);
You need to:
overload the constructor
public Product(String name){...}
or create instances of Product using the right and only one constructor uor have:
public Product(String name, int stock, double price)
if you overload then something like this should happen
public Product(String name){
this(name, 0, 0.0);
}
so you call a constructor from the other constructor
This is the time to learn constructor overloading. Overloading comes from OOP.
You can use Overloading to methods and constructors. Overloading means for a same method name you can implement that method
several time with different parameters(number of parameters)
. Actualy not only that,
you can use different data types for parameter.
also can change order of parameter.
keep remember method name must be same.
For the constructor also same thing. If you use for constructor you can add parameters like:
//constructor with one parameter
public Product(String name) {
this.name = name;
this.stock = 0;//or whatever your default value
this.price = 0;//or whatever your default value
}
//constructor with two parameter
public Product(String name, , int stock) {
this.name = name;
this.stock = stock;
this.price = 0;//or whatever your default value
}
public Product(String name, int stock, double price) {
this.name = name;
this.stock = stock;
this.price = price;
}
Like that you can add as many as you want.
Or you can use one constructor and pass argument to match with the implementation of the constructor when creating object. Like below:
product = new Product("Sticky tape", 0, 0);
this is not complete description you can read this to learn more
You have no constructor In Product class that takes single String argument. Create it like so:
public Product(String name) {
this.name = name;
}
In import statement you forgot semicolon:
import java.text.*;
your program is having 3 coding error which include
you forgot the " ; " after " import java.text.* " actually it is not required in your code, you can remove it, nothing will change.
you cannot make class Product as public , because you've made "Store" as your Primary class having main method.So remove public keyword from the Product class.
You didn't create a parameterized constructor
which should be like
public Product(String name){ this.name = name;}
in your product class.
your code will be like after correcting
class Product {
private String name;
private int stock;
private double price;
public Product(String name, int stock, double price) {
this.name = name;
this.stock = stock;
this.price = price;
}
public Product(String name) {
this.name = name;
}
public double sell(int n) {
stock = n;
return stock;
}
public void restock(int n) {
}
#Override
public String toString() {
return stock + name + "at $"+price;
}
}
public class Store {
public static void main(String[] args) {
Store s = new Store();
System.out.println(s.product);
System.out.println(s.cashRegister);
}
private Product product;
private Product cashRegister;
public Store() {
product = new Product("Sticky tape");
cashRegister = new Product("Cash register");
}
}
The errors are in these lines of code:
product = new Product("Sticky tape");
cashRegister = new Product("Cash register");
The Product constructor defined expects:
public Product(String name, int stock, double price)

How do I use toString to retrieve my information in my class and how do I get my method to calculate the "GPA" in this class?

My question is how do I get the gpa that I have listed in my constructor class to compile. Every time I try to compile it, I get a .class expected error. I'm rather new to java so please forgive my incompetence. My teacher wants us to calculate a a students gpa by dividing the number of points by classes in our constructor class. Then she wants us to use toString to retrieve our students information, which I do not know how to do as we have gone over toString once. Sorry if my explanation is a little confusing, but its hard to explain this class without sounding like its a class I'm teaching. Here is my code:
public class student
{
private String name;
private int year;
private int age;
private double gpa;
/*
* Default Constructor
*/
public student()
{
name = "John";
year = 2016;
age = 16;
gpa = 4.0;
}
/*
* other constructor
*/
public student(String name, int year, int age, double gpa)
{
this.name = name;
this.year = year;
this.age = age;
this.gpa = gpa;
}
/*
* accessors
*/
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
public int getYear()
{
return year;
}
public double getGpa()
{
return gpa;
}
/*
* mutators
*/
public void setName(String name)
{
this.name=name;
}
public void setGpa(double gpa)
{
this.gpa=gpa;
}
public void setYear(int year)
{
this.year=year;
}
public void setAge(int age)
{
this.age=age;
}
/*
* calculate GPA
*/
public double calcGpa(double points, int classes)
{
return double gpa;
gpa = points / classes;
}
}
calcGpa method can be something like this:
public double calcGpa(double points, int classes)
{
gpa = points / classes;
return gpa;
}
and for change the toString method you can override it. something like this:
#Override
public String toString() {
return getName() + " : " + getGpa()
}

How to reference a extended subclass in java?

I am currently working on a project for java and the teacher isn't very great at explaining himself.
Currently I'm stuck trying to reference an extended Class in java to get the balance. (i'm trying to edit the balance so that it can be displayed and changed in the array.
my main account looks like this
public class Account {
private String name;
private double quantity, price;
private double rate;
private Asset[] Asset;
}
The account that is extended is
abstract public class Asset {
String symbol;
public String getSymbol() {
return symbol;
}
protected Asset(String symbol) {
this.symbol = symbol;
}
}
The extensions look like so;
public class Cash extends Asset {
private double Quantity;
public Cash(double Quantity, String symbol) {
super(symbol);
this.Quantity = Quantity;
}
#Override
public String getSymbol() {
return super.getSymbol(); //To change body of generated methods, choose Tools | Templates.
}
public double getQuantity() {
return Quantity;
}
public void setQuantity(double Quantity) {
this.Quantity = Quantity;
}
}
Now lets say I have a test instance created of class Account.
How would I be able to edit the Cash value from the Class account? (Main arg)
Here are the other extensions I have too just for reference.
public class Stock extends Asset {
private String name;
private double quantity, rate;
public Stock(String name, double quantity, double rate, String symbol) {
super(symbol);
this.name = name;
this.quantity = quantity;
this.rate = rate;
}
#Override
public String getSymbol() {
return super.getSymbol(); //To change body of generated methods, choose Tools | Templates.
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getQuantity() {
return quantity;
}
public void setQuantity(double quantity) {
this.quantity = quantity;
}
public double getRate() {
return rate;
}
public void setRate(double rate) {
this.rate = rate;
}
}
If it helps, how would I be able to change the values in the extended classes?
I've tried
Asset[0] = new Cash(25000.00,string);
but that's only helpful in putting the value manually.
Apologies for any mistakes, I'm pretty new to coding and java in general.
The way you named your Asset array is confusing, java is case sensitive so name it for example:
private Asset[] asset;
You need to get a reference of the right type through casting before you can access it's own methods. You can do for example:
if (asset[0] instanceof Cash) {
Cash cash = (Cash)asset[0];
// Then you can access the setQuantity method
cash.setQuantity(25000.00);
}

Categories