Question:
How to set / get in Java?
2013-01-28 19:38:21 UTC
I don't know if it's just me or these instructions, but I have been digging around my textbook and online. I can't seem to find a concrete way to do this problem:

Problem A: Antique Toy Inventory . Objectives: Creating classes, passing parameters to objects from a driver, retrieving data from an object.
Write a class named AntiqueToys which represents an object that tracks a particular toy in your collection of antique toys. It has the following attributes: description, maker, year made, value.
Write appropriate mutator (set) methods which update the attribute values, and accessor (get) methods which return the attribute values. Create a driver program which creates three toy objects, stores the following values in each, and then displays the values for each toy. Read at least one toy's data from the keyboard. Data for the other two may be 'hard-coded' as literals in your driver.
Description Maker Year Made Value
Electric Train Lionel 1934 $459.95
Handcar Coin Bank J&E Stevens 1883 $2,195.00
Stuffed Bear Steiff 1905 $214.00

Any help is greatly appreciated.
Four answers:
John
2013-01-29 01:57:13 UTC
package com.example.toys;



import java.util.Scanner;



public class AntiqueToyCompany {



public static void main(String[] args) {

Toy[] toys = new Toy[3];

toys[0] = new Toy("Electric Train Lionel", 1945, 459.95d);

toys[1] = new Toy("Handcar Coin Bank J&E Stevens", 1833, 2195d);



Scanner keyboard = new Scanner(System.in);

System.out.print("Enter maker: ");

String maker = keyboard.nextLine();

System.out.print("Enter year made: ");

int yearMade = keyboard.nextInt();

System.out.print("Enter a value: ");

double value = keyboard.nextDouble();



toys[2] = new Toy(maker, yearMade, value);



for (Toy toy : toys) {

System.out.println(toy);

}



System.exit(0);

}

}



class Toy {

String maker;

int yearMade;

double value;



public Toy(String maker, int yearMake, double value) {

setMaker(maker);

setYearMade(yearMake);

setValue(value);

}



public String getMaker() {

return maker;

}



public void setMaker(String maker) {

this.maker = maker;

}



public int getYearMade() {

return yearMade;

}



public void setYearMade(int yearMade) {

this.yearMade = yearMade;

}



public double getValue() {

return value;

}



public void setValue(double value) {

this.value = value;

}



@Override

public String toString() {

StringBuilder sb = new StringBuilder();

sb.append("Maker: ").append(getMaker());

sb.append(", Year made: ").append(getYearMade());

sb.append(", Value: ").append(String.format("$%,.2f", getValue()));

return sb.toString();

}

}



/*

Enter maker:Stuffed Bear Steiff

Enter year made: 1905

Enter a value: 214

Maker: Electric Train Lionel, Year made: 1945, Value: $459.95

Maker: Handcar Coin Bank J&E Stevens, Year made: 1833, Value: $2,195.00

Maker: Stuffed Bear Steiff, Year made: 1905, Value: $214.00

*/
koob
2016-08-11 02:35:40 UTC
Here you'll have set the trail of 'BIN' listing of JAVA for instance : c:software filesjavajdk1.5bin this is class direction for JAVA. Add this route in environment variables.
2013-01-28 19:44:54 UTC
Here is a small version of the Antique Toys Class, hope it helps:

public class AntiqueToys{

private String description, maker;

private int year_made;

private double value;

//empty default constructor

public AntiqueToys(){

description="";

maker="";

year_made=0;

value=0;

}

//Example of Mutator

void setDescription(String desc)

{

description=desc;

return;

}

//Example of accessor

String getDescription()

{

return description;

}

}
2013-01-28 22:09:21 UTC
What the guy above me said should work. Where are you getting these problems? I would love to use them as 'training'


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...