I need help with how to get those private final values from the MobileServiceProvider Class to make it works to my MobileSericeProviderTest Class.
Description: Design a class that calculates a customer's monthly bill.It should store the letter of the package the customer has purchased (A, B, or C) and the number of miniutes that were used. it should have a method that returns the total charges. Demonstrate the class in a program that asks the user to select a package and enter the number of minutes used. The program should display the total charges.
package mobileserviceprovider;
public class MobileServiceProvider
{
private final double COST_A = 39.99;
private final double COST_B = 59.99;
private final double COST_C = 69.99;
private final int MIN_A = 450;
private final int MIN_B = 900;
private final double OVER_A = 0.45;
private final double OVER_B = 0.40;
String customer_Package;
double customer_TimeUse;
MobileServiceProvider() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
public double getCostA()
{
return COST_A;
}
public int getMinA()
{
return MIN_A;
}
public String getPackage()
{
return customer_Package;
}
public double getHours()
{
return customer_TimeUse;
}
public void setPackage( String customer_Package )
{
this.customer_Package = customer_Package;
}
public void setHours( double customer_TimeUse )
{
this.customer_TimeUse = customer_TimeUse;
}
public MobileServiceProvider( String pack, double time )
{
customer_Package = pack;
customer_TimeUse = time;
{
}
}
}
}
package mobileserviceprovider;
import java.util.Scanner;
public class MobileServiceProviderTest
{
public static void main(String[] args)
{
String enter_Package = null;
double enter_Hours;
double min_A;
double bill;
Scanner keyboard = new Scanner(System.in);
System.out.print("What is your package type?");
keyboard.nextLine();
System.out.print("How many hours did you use for this package?");
enter_Hours = keyboard.nextDouble();
keyboard.nextLine();
MobileServiceProvider calMobileSP = new MobileServiceProvider();
calMobileSP.setPackage(enter_Package);
calMobileSP.setHours(enter_Hours);
calMobileSP.getMinA();
switch ( enter_Package)
{
case "A":
{
if (enter_Hours <= MIN_A)
bill = COST_A;
else
bill = COST_A + OVER_A * (enter_Hours - MIN_A)
}
}
}
}
Related
So I am currently learning about interfaces within java and in this program I created 3 separate classes Building.class, Bicycle.class, and Car.class and they are unrelated but they all use the CarbonFootPrint Interface. in my processCarbonFootPrintData class I created an arrayList that holds the data from my objects then I loop through the array list and I get this weird output that does not show the result of my input data.
package CarbonFootPrintPackage;
import java.util.Scanner;
/**
*
* #author cjt1496
*/
public class Building implements CarbonFootPrintInterface {
private int numberOfFloors;
private int numberOfJanitors;
private boolean isBuildingOpenOrClosed;
double naturalGasConsumed;
Scanner input = new Scanner(System.in);
public double getNaturalGasConsumed() {
return naturalGasConsumed;
}
public void setNaturalGasConsumed(double naturalGasConsumed) {
this.naturalGasConsumed = naturalGasConsumed;
}
public int getNumberOfFloors() {
return numberOfFloors;
}
public void setNumberOfFloors(int numberOfFloors) {
this.numberOfFloors = numberOfFloors;
}
public int getNumberOfJanitors() {
return numberOfJanitors;
}
public void setNumberOfJanitors(int numberOfJanitors) {
this.numberOfJanitors = numberOfJanitors;
}
public boolean isIsBuildingOpenOrClosed() {
return isBuildingOpenOrClosed;
}
public void setIsBuildingOpenOrClosed(boolean isBuildingOpenOrClosed) {
this.isBuildingOpenOrClosed = isBuildingOpenOrClosed;
}
public Building(){
}
public Building(int numberOfFloors, int numberOfJanitors, boolean isBuildingOpenOrClosed, double naturalGasConsumed) {
this.numberOfFloors = numberOfFloors;
this.numberOfJanitors = numberOfJanitors;
this.isBuildingOpenOrClosed = isBuildingOpenOrClosed;
this.naturalGasConsumed = naturalGasConsumed;
}
public void calculateCarbonFootPrint(){
System.out.println("Now Calculating Carbon foot print for a Building ");
System.out.println("--------------------------------------------------------");
System.out.println("How many therms of natural gas has your building consumed?");
naturalGasConsumed = input.nextDouble();
}
#Override
public void getCarbonFootPrint() {
System.out.println("The carbon foot print emitted from this building is " +
(getNaturalGasConsumed() * 11.7) + "pounds of CO2 from natural gas use.\n");
}
}
START OF CAR.CLASS
public class Car implements CarbonFootPrintInterface {
private int numberOfSeats;
private int steeringWheel;
double emissionConversionFactor;
double distanceTraveled;
int numberOfTimesTraveled;
Scanner input = new Scanner(System.in);
public int getNumberOfSeats() {
return numberOfSeats;
}
public void setNumberOfSeats(int numberOfSeats) {
this.numberOfSeats = numberOfSeats;
}
public int getSteeringWheel() {
return steeringWheel;
}
public void setSteeringWheel(int steeringWheel) {
this.steeringWheel = steeringWheel;
}
public double getEmissionConversionFactor() {
return emissionConversionFactor;
}
public void setEmissionConversionFactor(double emissionConversionFactor) {
this.emissionConversionFactor = emissionConversionFactor;
}
public double getDistanceTraveled() {
return distanceTraveled;
}
public void setDistanceTraveled(double distanceTraveled) {
this.distanceTraveled = distanceTraveled;
}
public int getNumberOfTimesTraveled() {
return numberOfTimesTraveled;
}
public void setNumberOfTimesTraveled(int numberOfTimesTraveled) {
this.numberOfTimesTraveled = numberOfTimesTraveled;
}
public Car(){
}
public Car(int numberOfSeats, int steeringWheel, double emissionConversionFactor, double distanceTraveled, int numberOfTimesTraveled) {
this.numberOfSeats = numberOfSeats;
this.steeringWheel = steeringWheel;
this.emissionConversionFactor = emissionConversionFactor;
this.distanceTraveled = distanceTraveled;
this.numberOfTimesTraveled = numberOfTimesTraveled;
}
public void calculateCarbonFootPrint(){
System.out.println("Now Calculating Carbon foot print for a Car ");
System.out.println("--------------------------------------------------------");
System.out.println("Enter your emissionConversionFactor (Must be a decimal)");
emissionConversionFactor = input.nextDouble();
System.out.println("Enter your distance traveled in km (Must be a decimal)");
distanceTraveled = input.nextDouble();
System.out.println("Enter the number of times you traveled to your destination");
numberOfTimesTraveled = input.nextInt();
}
#Override
public void getCarbonFootPrint() {
System.out.println("The carbon foot print emitted from this bicycle is " +
getEmissionConversionFactor() * (getDistanceTraveled() * getNumberOfTimesTraveled()) +"Kg CO2e\n");
}
}
START OF BICYCLE.CLASS
import java.util.Scanner;
public class Bicycle implements CarbonFootPrintInterface {
private int handleBars;
private boolean KickStand;
double emissionConversionFactor;
double distanceTraveled;
int numberOfTimesTraveled;
Scanner input = new Scanner(System.in);
public int getHandleBars() {
return handleBars;
}
public void setHandleBars(int handleBars) {
this.handleBars = handleBars;
}
public boolean isKickStand() {
return KickStand;
}
public void setKickStand(boolean KickStand) {
this.KickStand = KickStand;
}
public double getEmissionConversionFactor() {
return emissionConversionFactor;
}
public void setEmissionConversionFactor(double emissionConversionFactor) {
this.emissionConversionFactor = emissionConversionFactor;
}
public double getDistanceTraveled() {
return distanceTraveled;
}
public void setDistanceTraveled(double distanceTraveled) {
this.distanceTraveled = distanceTraveled;
}
public int getNumberOfTimesTraveled() {
return numberOfTimesTraveled;
}
public void setNumberOfTimesTraveled(int numberOfTimesTraveled) {
this.numberOfTimesTraveled = numberOfTimesTraveled;
}
public Bicycle(){
}
public Bicycle(int handleBars, boolean KickStand, double emissionConversionFactor, double distanceTraveled, int numberOfTimesTraveled) {
this.handleBars = handleBars;
this.KickStand = KickStand;
this.emissionConversionFactor = emissionConversionFactor;
this.distanceTraveled = distanceTraveled;
this.numberOfTimesTraveled = numberOfTimesTraveled;
}
public void calculateCarbonFootPrint(){
System.out.println("Now Calculating Carbon foot print for Bicycle ");
System.out.println("--------------------------------------------------------");
System.out.println("Enter your emissionConversionFactor (Must be a decimal)");
emissionConversionFactor = input.nextDouble();
System.out.println("Enter your distance traveled in km (Must be a decimal)");
distanceTraveled = input.nextDouble();
System.out.println("Enter the number of times you traveled to your destination");
numberOfTimesTraveled = input.nextInt();
}
#Override
public void getCarbonFootPrint() {
System.out.println("The carbon foot print emitted from this bicycle is " +
getEmissionConversionFactor() * (getDistanceTraveled() * getNumberOfTimesTraveled()) +"Kg CO2e\n");
}
START Of PROCESS_CARBON_FOOTPRINT_DATA CLASS
public class ProcessCarbonFootPrintData {
public void createCarbonFootPrint(){
Building newBuilding = new Building();
Car newCar = new Car();
Bicycle newBicycle = new Bicycle();
newBuilding.calculateCarbonFootPrint();
newCar.calculateCarbonFootPrint();
newBicycle.calculateCarbonFootPrint();
ArrayList footPrint = new ArrayList();
footPrint.add(newBuilding);
footPrint.add(newCar);
footPrint.add(newBicycle);
for (Object footPrint1 : footPrint) {
System.out.println(footPrint1.toString());
}
}
}
This is the output I am getting:
CarbonFootPrintPackage.Building#42a57993
CarbonFootPrintPackage.Car#75b84c92
CarbonFootPrintPackage.Bicycle#6bc7c054
ArrayList footPrint = new ArrayList();
footPrint.add(newBuilding);
footPrint.add(newCar);
footPrint.add(newBicycle);
for (Object footPrint1 : footPrint) {
System.out.println(footPrint1.toString());
}
Your arraylist contains Objects, it doesn't know anything further of the type. When you do:
for ( Object footPrint1 : footPrint) {
}
You also declare the elements to be of type Object.
There are two things you need to do:
Be specific about the type. If you want to keep your List as is, with the different types, change your loop to:
for ( Object footPrint1 : footPrint) {
if ( footPrint1 instanceof Car )
System.out.println((Car)footPrint1);
else if ( footPrint1 instanceof Building )
System.out.println((Building)footPrint1);
else System.out.println((Bicycle)footPrint1);
}
This way, it'll know what type of data to print.
By just doing that, you'll still run into the same issue, because you haven't overridden your toString methods.
Add the following to your Car class:
#Override
public String toString() {
return "I am a car!!";
}
and you'll see that for the Car instance, that line is printed, instead of the memory address.
Override that method for all your classes, and alter the value returned by it the way you want it to be.
I am building a program with a class Item (in Item.java) and class Receipt (in Receipt.java). They both are in the same package. I want the Receipt constructor method to initialize with an ArrayList of instances of the Item object. How can I accomplish this? I keep getting a "cannot find symbol" error when I compile my code / run the Receipt.java file.
Receipt.java
package com.calculator;
import java.util.ArrayList;
// Receipt model
public class Receipt {
public ArrayList<Item> items;
// initialized with a list of item objects
public Receipt(ArrayList<Item> lineItems) {
items = lineItems;
}
// calculates total
public double totalWithSalesTax() {
}
// calculates total sales tax
public double totalSalesTax() {
double salesTax = 0;
for (Item item: items) {
salesTax = salesTax + item.calculateTax();
}
return salesTax;
}
// goes through each item and creates a string that you'd see on the receipt output
public static void main(String[] args) {
Item one = new Item("1 packet of headache pills at 9.75");
Item two = new Item("1 bottle of perfume at 18.99");
Item three = new Item("1 box of imported chocolates at 11.25");
ArrayList<Item> list = new ArrayList<>();
list.add(one);
list.add(two);
list.add(three);
System.out.println(list);
}
}
how i'm calling my code in the Receipt.java main. I get the same "cannot find symbol" error on those lines when I call them:
public static void main(String[] args) {
// the Item class is initialized with a string
Item i = new Item("1 imported box of chocolates at 10.00");
System.out.println(i.isImported);
System.out.println(i.isExempt);
System.out.println(i.quantity);
System.out.println(i.productName);
System.out.println(i.initialPrice);
System.out.println(i.calculateTax());
System.out.println(i.totalItemPriceWithTax());
}
I expected the program to recognize Item as an object in the program because they are in the same class. But I keep getting a "cannot find symbol" error when I compile my code.
For those asking about the Item class:
package com.calculator;
import java.util.ArrayList;
public class Item {
// instance variables
private boolean isImported = false;
private boolean isExempt = false;
private String productName;
private int quantity;
private double initialPrice;
// class variables
private static ArrayList<String> exemptItems = new ArrayList<String>();
// create a list of exempt items
static {
exemptItems.add("book");
exemptItems.add("chocolate");
exemptItems.add("pills");
}
public Item(String input) {
String[] strSplit = input.split(" at ");
// set initial price
initialPrice = Double.parseDouble(strSplit[1]);
// set quanitity
quantity = Integer.parseInt(strSplit[0].substring(0, strSplit[0].indexOf(" ")));
// set productname
String[] description = strSplit[0].split(" ", 2);
productName = description[1];
// set isExempt & isImported
setImported();
setExempt();
}
// method that checks if isImported
private void setImported() {
if (productName.contains("imported")) {
isImported = true;
}
}
// method that checks if isExempt
private void setExempt() {
if (getExemptItems().parallelStream().anyMatch(productName::contains)) {
isExempt = true;
}
}
// write a method that determines how much tax per item
public double calculateTax() {
double salesTax = 0.10;
double importTax = 0.05;
double precision = 0.05;
double tax = 0;
if (isImported) {
tax = tax + (initialPrice * importTax);
}
if (!isExempt) {
tax = tax + (initialPrice * salesTax);
}
// rounding to nearest .05
tax = Math.ceil(tax / precision) * precision;
return tax;
}
// write a method that represent total with tax
private double totalItemPriceWithTax() {
return this.calculateTax() + initialPrice;
}
private static ArrayList<String> getExemptItems() {
return exemptItems;
}
public static void main(String[] args) {
}
} ```
Probably you are getting this error because of this:
package com.calculator.*;
Package keyword does not support wildcards.
i have a problem on my program. and my problem is that i cannot minus my withdrawal from my deposit value.
code below:
public static void main(String[] args) {
double cash;
boolean more = true;
Deposite dep = new Deposite();
Withdraw with = new Withdraw();
while (more) {
cash = Double.parseDouble(JOptionPane.showInputDialog("Cash Deposite"));
dep.Deposite(cash);
dep.print();
int con = JOptionPane.YES_NO_OPTION;
int con1 = JOptionPane.showConfirmDialog(null, "Do you want more Deposites?","DEPOSITORY",con);
if (con1 == 1) {
int con3 = JOptionPane.showConfirmDialog(null, "Withdraw now?","WITHDRAWAL",con);
if (con3 == 0) {
cash = Double.parseDouble(JOptionPane.showInputDialog("Cash Withdraw"));
with.Withdraw(cash);
with.print();
System.out.println("Thanks");
}
}
}
}
and this is my subclass that i have made for its functions
public class Deposite {
private double depcash;
public double Deposite(double cash){
depcash += cash;
return this.depcash;
}
void print(){
System.out.printf("Your deposite is $%5.2f",depcash);
System.out.println(" ");
}
}
and this is for my withdrawal class. i inherit it. but i still dont know how it works.
code below :
public class Withdraw extends Deposite {
double cash;
public double Withdraw(double withdraw){
super.Deposite(withdraw);
cash -=withdraw;
return cash;
}
void print (){
System.out.printf("You Cash Balance now is $%5.2f",cash);
System.out.println(" ");
}
}
First of all, never name your methods like object constructors
public double Deposite(double cash).
Secondly, why would your Withdraw class extend Deposite? Is there any reason for this?
That is how I would implement some banking logic:
Bank bank = new Bank();
Account account = new Account(123.50);
bank.execute(account, new Deposit(), 1);
bank.execute(account, new Withdraw(), 13.50);
private static interface Operation {
double apply(Account account, double value);
}
private static class Deposit implements Operation {
#Override
public double apply(Account account, double value) {
return account.getMoney() - value;
}
}
private static class Withdraw implements Operation {
#Override
public double apply(Account account, double value) {
return account.getMoney() + value;
}
}
private static class Account {
private final double money;
public Account(double money) {
this.money = money;
}
public double getMoney() {
return money;
}
}
private static class Bank {
public void execute(Account account, Operation operation, double amount) {
operation.apply(account, amount);
}
}
Your program have some basic problem here is the code:::
You should have made the single account for deposit and withdraw. That was your basic mistake.
import javax.swing.JOptionPane;
public class Bank {
public static double totalCash = 0;
public static void main(String[] args) {
boolean more = true;
Deposite dep = new Deposite();
Withdraw with = new Withdraw();
while (more) {
double cash = Double.parseDouble(JOptionPane.showInputDialog("Cash Deposite"));
dep.depositeCash(cash);
dep.print();
int con = JOptionPane.YES_NO_OPTION;
int con1 = JOptionPane.showConfirmDialog(null, "Do you want more Deposites?", "DEPOSITORY", con);
if (con1 == 1) {
int con3 = JOptionPane.showConfirmDialog(null, "Withdraw now?", "WITHDRAWAL", con);
if (con3 == 0) {
cash = Double.parseDouble(JOptionPane.showInputDialog("Cash Withdraw"));
with.withdrawCash(cash);
with.print();
System.out.println("Thanks");
more = false;
}
}
}
}
}
class Withdraw {
public double withdrawCash(double withdraw) {
Bank.totalCash -= withdraw;
return Bank.totalCash;
}
void print() {
System.out.printf("You Cash Balance now is $%5.2f", Bank.totalCash);
System.out.println(" ");
}
}
class Deposite {
public double depositeCash(double cash) {
Bank.totalCash += cash;
System.out.println(Bank.totalCash);
return Bank.totalCash;
}
void print() {
System.out.printf("Your deposite is :" + Bank.totalCash);
System.out.println(" ");
}
}
When you do
Deposite dep = new Deposite();
Withdraw with = new Withdraw();
it creates two different instances. One of Deposite and one of Withdraw.
You assume that both instances share the same double cash , but they don't.
If you want to start with something simple you could do something like :
import javax.swing.JOptionPane;
public class Cash {
private double depcash;
public double deposite(double cash){ //stick to java naming conventions
depcash += cash;
return depcash;
}
public double withdraw(double withdraw){
return deposite(- withdraw);
}
void print(){
//wrong - System.out.printf("Your deposite is $%5.2f",depcash);
System.out.printf("Your cash balance is $%5.2f",depcash);
System.out.println(" ");
}
public static void main(String[] args) {
double sum;
boolean more = true;
Cash cash = new Cash();
while (more) { //how do you stop ? what makes more false ?
sum = Double.parseDouble(JOptionPane.showInputDialog("Cash deposite"));
cash.deposite(sum);
cash.print();
int con = JOptionPane.YES_NO_OPTION;
int con1 = JOptionPane.showConfirmDialog(null, "Do you want more Deposites?","DEPOSITORY",con);
if (con1 == 1) {
int con3 = JOptionPane.showConfirmDialog(null, "Withdraw now?","WITHDRAWAL",con);
if (con3 == 0) {
sum = Double.parseDouble(JOptionPane.showInputDialog("Cash Withdraw"));
cash.withdraw(sum);
cash.print();
System.out.println("Thanks");
}
}
}
}
}
I am creating an object of a class from 2 separate classes and both objects are returning different values for the same method. I suspect it may be an issue with the while loop but here are the classes. The main class works, the setup class is the class that is being turned into and object and the game loop class has the object that doesn't return the right values. it returns the values defined at the beginning of setup and not the modified versions.
import java.util.Scanner;
public class MainClass {
static Scanner input = new Scanner(System.in);
//String x = input.nextLine();
public static void main(String[] args)
{
setup setupGov = new setup();
gameLoop gameLoop = new gameLoop();
setupGov.statsSetup();
System.out.println("happyness: " + setupGov.getHappyness() + " money: £" + setupGov.getMoney() + " population: " + setupGov.getPopulation());
gameLoop.loop();
}
}
import java.util.Scanner;
public class setup {
static Scanner input = new Scanner(System.in);
String goverment;
int happyness;
double money;
int population = 1000000;
public setup()
{
}
public void statsSetup()
{
System.out.println("Choose a goverment: 1. democracy 2. monarchy 3. dictatorship");
goverment = input.nextLine();
if (goverment.equals("1"))
{
happyness = 75;
money = 250000.0;
}
else if (goverment.equals("2"))
{
happyness = 50;
money = 500000.0;
}
else if (goverment.equals("3"))
{
happyness = 25;
money = 750000.0;
}
else
{
System.out.println("ENTER A VALID VALUE");
}
}
public int getHappyness()
{
return happyness;
}
public double getMoney()
{
return money;
}
public int getPopulation()
{
return population;
}
}
import java.util.Scanner;
public class gameLoop
{
static Scanner input = new Scanner(System.in);
static int turn = 0;
int happyness;
double money;
int population;
public gameLoop()
{
}
setup setupGov = new setup();
public static void main(String[] args)
{
}
public void loop()
{
while (true)
{
System.out.println("Turn: "+turn);
input.nextLine();
turn++;
}
}
}
You are creating two different instances of class setup. One is created directly in main function and other is created in gameLoop object. They do not share their attributes so methods may return different value. Every time you use 'new' operator, a new instance of class is created with it's own attributes (only static member are shared since static member belongs to class instead of instances). If you want to have same instances you could write:
public class gameLoop
{
static Scanner input = new Scanner(System.in);
static int turn = 0;
int happyness;
double money;
int population;
public gameLoop(setup setupGov)
{
this.setupGov = setupGov;
}
setup setupGov;
public static void main(String[] args)
{
}
public void loop()
{
while (true)
{
System.out.println("Turn: "+turn);
input.nextLine();
turn++;
}
}
}
And in main:
public class MainClass {
static Scanner input = new Scanner(System.in);
//String x = input.nextLine();
public static void main(String[] args)
{
setup setupGov = new setup();
gameLoop gameLoop = new gameLoop(setupGov);
setupGov.statsSetup();
System.out.println("happyness: " + setupGov.getHappyness() + " money: £" + setupGov.getMoney() + " population: " + setupGov.getPopulation());
gameLoop.loop();
}
}
Now both of objects setupGov will be the same instance.
Please note:
It is good practice to have class name written with capitalized first letter eg. GameLoop instead of gameLoop
I don't really understand what you're trying to do or what the question is, but in your main class you have an object with the same exact name of the class.
gameLoop gameLoop = new gameLoop();
I don't know if that's the exact cause of your problem, but I'm almost sure that that isn't supposed to be like that.
Hello So I have a entire class called tractor with different data's stored in it but now I'm suppose to create an object call tractor with a zero parameter constructor but This is the code I have so far and its giving em errors
First off this my Tractor Class which is in a different file:
import java.util.Scanner;
class Tractor
{
private int RentalRate;
private int RentalDays;
private int VehicleID;
private int RentalProfit;
public void setRentalRate(int r)
{
Scanner input = new Scanner(System.in);
System.out.println("What's the Rental Rate?");
int num = input.nextInt();
num = r;
if(r<0 || r >1000)
RentalRate = r;
RentalRate= 1;
}
public int getRentalRate()
{
return RentalRate;
}
public void setVehicleID(int v)
{
Scanner input = new Scanner(System.in);
System.out.println("What's the vehicleID?");
int num1 = input.nextInt();
num1 = v;
if(v<0)
VehicleID = v;
VehicleID = 1;
}
public int getVehicleID()
{
return VehicleID;
}
public void setRentalDays(int d)
{
Scanner input = new Scanner(System.in);
System.out.println("How many rental days?");
int num2 = input.nextInt();
num2 = d;
if(d<0)
RentalDays = d;
RentalDays = 1;
}
public int getRentalDays()
{
return RentalDays;
}
public String toString()
{
String str;
str = "RentalDays:" + RentalDays +"\nRenalRate:" + RentalRate + "\nVehicleID " + VehicleID;
return str;
}
public void RentalProfit(int RentalRate, int RentalDays)
{
RentalProfit = RentalRate * RentalDays;
}
}
import java.util.Scanner;
public class testTractor
{
public static void main(String[] args)
{
public tractor()
{
this.RentalDays = d;
this.RentalRate = r;
this.VehicleID = v;
}
}
}
The error is :
testTractor.java:7: error: illegal start of expression
public tractor()
^
testTractor.java:7: error: ';' expected
public tractor()
^
2 errors
You have compilation errors. You need to first declare the Tractor class then add the constructor inside it. One way to do is declare in a separate file. Also in Java unless you had defined d you couldnt have assigned it. Maybe you wanted to assign the day as a String look in the examples I provide below.
You need to to first create a file call Tractor.java and then define variables there. For example contents of Tractor.java:
public class Tractor {
String rentaldays,someOtherValue;
public Tractor(){
rentaldays ="monday";
someOtherValue="value";
}
//or
public Tractor(String rentalDays){
this.rentaldays = rentalDays;
someOtherValue = "asf";
}
}
Then in your main method You can do Tractor trac = new Tractor(); or Tractor trac = new Tractor("tuesday"); also after that you can print the rentaldays of trac using System.out.println(trac.rentaldays);
From the looks of it you will probably be making a tractor rental system. In that case, rentalDays may be an array of Strings. And then you would have an array of Tractor objects to store in the rental system. You can look at these terms and keywords to point you in the right direction.
You are defining it wrong, define your methods inside class then call them in main() method.
class Test{
public void greeting(){
System.out.print("hello to JAVA..");
}
public static void main(String[] args){
Test testObj = new Test();
testObj.greeting();
}
}
you use an illegal of java syntax, if you already have class tractor in your project. for calling it to in other class, try below code
public class TestTractor(){
Tractor objTractor;
public static void main(String[] args){
//create new tractor object with no parameter
objTractor = new Tractor();
//create new tractor object with parameter
objTractor = new Tractor(parameter here);
//do some action of object here
...........
}
}
//This is just a sample
in your tractor class add below code
public tractor()
{
this.RentalDays = d;
this.RentalRate = r;
this.VehicleID = v;
}
And keep your TestTractor class as
public class TestTractor(){
public static void main(String[] args){
Tractor objTractor = new Tractor();
// objTractor.yourMethodName
}
}