I understand that I do not have anything in my main method yet so nothing will execute, that is the second part to the assignment.
My question is, I'm getting an error in the computeDiscount method where I'm calling the computeNumCases method. The error i'm getting is
variable numCases might not have been initialized
so I need help figuring that error out.
Also, help in the computeCost method would be very helpful as well.
public class Customer {
private String customerLastName;
private String candyType;
private int numCandyBars;
private double costPerBar;
private int NUMBERPERCASE = 12;
public static void main(String[] args) {
}
public Customer() {
}
public Customer(String name, String type, int numCandyBars, double costPerBar) {
//this.name = name;
candyType = type;
this.numCandyBars = numCandyBars;
this.costPerBar = costPerBar;
}
public static double getCostPerBar(double costPerBar) {
return costPerBar;
}
public static int getNumCandyBars(int numCandyBars) {
return numCandyBars;
}
public String getCandyType() {
return candyType;
}
public String GetCustomerLastName() {
return customerLastName;
}
public void setCustomerLastName(String name) {
this.customerLastName = name;
}
public void setNumCandyBars(int num) {
this.numCandyBars = num;
}
public void setCandyType(String candyType) {
this.candyType = candyType;
}
public void setCostPerBar(double cost) {
costPerBar = cost;
}
public int computeNumCases(int numCases) {
numCases = numCandyBars / NUMBERPERCASE;
return numCases;
}
public int computeNumIndividuals(int numIndividuals) {
numIndividuals = numCandyBars % NUMBERPERCASE;
return numIndividuals;
}
public int computeDiscount(int discount) {
int numCases = computeNumCases(numCases);
if (numCases < 20)
discount = 100;
else if (numCases < 50)
discount = 85;
else
discount = 75;
return discount;
}
public double computeCost(double totalCost) {
return computeNumCases(numCases) * getCostPerBar(costPerBar) * (NUMBERPERCASE * discount / 100.00) + computeNumIndividuals(numIndividuals) * getCostPerBar(costPerBar);
}
}
Related
I am this close to finally completing this assignment, but I've hit a roadblock. For some reason I can't get call aspects of the class in my methods. Does anyone have a way of doing this that works? I've put comments next to the three parts I'm referring to. I do not know what is causing these errors, and Im grateful for any help.
public class Main
{
public static int w = -1;
public static int x = 0;
public static int y = 0;
public static int z = 0;
public static String[] names;
public static int[] years;
public static String[] studios;
public static class Movie{
// instance variables
private int year;
private String title;
private String studio;
private int placement;
// Constructor for objects of class Movie
Movie(String title, int year, String studio)
{
// initialize instance variables
this.title = title;
this.year = year;
this.studio = studio;
}
public String getTitle()
{
return title;
}
public void setTitle(String title)
{
this.title = title;
}
public void setPlacement() {
w+=1;
this.placement = w;
}
public int getPlacement() {
return placement;
}
public String getStudio()
{
return studio;
}
public void setStudio(String studio)
{
this.studio = studio;
}
public int getYear()
{
return year;
}
public void setYear(int year)
{
this.year = year;
}
public String toString()
{
String str = String.format("%-30s %4d %-20s", title, year, studio);
return str;
}
}
public static void arrNames(String name) {
x+=1;
names = new String[x];
names[(x-1)] = name.getTitle(); //error here, why can't I just use getTitle()?
}
public static void arrYear(String name) {
y+=1;
years = new int[y];
years[(y-1)] = name.getYear(); //same error
}
public static void arrStudio(String name) {
z+=1;
studios = new String[z];
studios[z-1] = name.getStudio(); //same error
}
public static void setGroup(String name) {
arrNames(name);
arrYear(name);
arrStudio(name);
}
public static void getGroups(String name) {
int a = x;
int b = y;
int c = z;
int d = 0;
int e = 0;
int f = 0;
System.out.println("Names Category: ");
while (a > 0) {
System.out.print(names[d] + " ");
d+=1;
a-=1;
}
System.out.println("Years Category: ");
while (b > 0) {
System.out.print(years[e] + " ");
e+=1;
b-=1;
}
System.out.println("Studios Category: ");
while (c > 0) {
System.out.print(studios[f] + " ");
f+=1;
c-=1;
}
public static void main(String[] args) {
Movie nemo = new Movie("Finding Nemo",2003,"Pixar");
setGroup(nemo.getTitle());
}
}
I will take this exemple:
public static void arrYear(String name) {
y+=1;
years = new int[y];
years[(y-1)] = name.getYear(); //same error
}
You are trying to use the method getYear on the variable name and the name is a String and not a Movie Object
You could just replace the argument from String to Movie like that:
public static void arrYear(Movie name) {
y+=1;
years = new int[y];
years[(y-1)] = name.getYear(); //same error
}
and it should work for all your functions
If the quantity is not positive it should be set to 0.
If the price per item is not positive it should be set to 0.0.
Hwever when I enter a negative number it keeps setting my variable negative and not Zero.
This is my class package com.company;
/**
* Created by juliodiaz on 5/7/16.
*/
public class Invoice {
private String partNumber;
private String partDescription;
private int partQuantity;
private double partPrice;
public Invoice(String partNumber, String partDescription, int partQuantity, double partPrice) {
this.partNumber = partNumber;
this.partDescription = partDescription;
this.partQuantity = partQuantity;
this.partPrice = partPrice;
}
public String getPartNumber() {
return partNumber;
}
public void setPartNumber(String partNumber) {
this.partNumber = partNumber;
}
public String getPartDescription() {
return partDescription;
}
public void setPartDescription(String partDescription) {
this.partDescription = partDescription;
}
public int getPartQuantity() {
return partQuantity;
}
public void setPartQuantity(int partQuantity) {
this.partQuantity = partQuantity;
}
public double getPartPrice() {
return partPrice;
}
public void setPartPrice(double partPrice) {
this.partPrice = partPrice;
}
public double invoiceAmountMethod(double partPrice, int partQuantity) {
if (partQuantity < 0 || partPrice < 0.0) {
this.partQuantity = 0;
return partPrice * partQuantity;
}
else
return partPrice * partQuantity;
}
}
//Main method
package com.company;
public class Main {
public static void main(String[] args) {
// write your code here
Invoice myTruck = new Invoice("A101", "Wheels", -2, 100.00);
System.out.println(myTruck.getPartDescription());
System.out.println(myTruck.getPartNumber());
System.out.println(myTruck.getPartQuantity());
System.out.println(myTruck.getPartPrice());
double price = myTruck.getPartPrice();
int quantity = myTruck.getPartQuantity();
System.out.println("The total cost is " + myTruck.invoiceAmountMethod(price, quantity));
}
}
OUTPUT
Wheels
A101
-2
100.0
The total cost is -200.0
You're assigning:
this.partQuantity = 0;
but you return:
return partPrice * partQuantity;
(the parameters that were passed to the method).
you can fix it by returning:
return this.partPrice * this.partQuantity;
and you really shouldn't pass any parameters to this method.
Here is what you need.
public double invoiceAmountMethod(double partPrice, int partQuantity) {
return (partQuantity < 0 || partPrice < 0.0)?0.0:partPrice * partQuantity;
}
In Java, my basketball players keep sending back "infinity" in my getStats method and I do not know why. Can someone help please? I needed getters and setters so I have that. I am suppose to test the getStats() methods but it errors out every time. At first it was NaNa now its infinity
public class BasketBallPlayer
{
// instance variables - replace the example below with your own
private String name;
private int height;
private int weight;
private double freeThrowsAttempted;
private double freeThrowsMade;
private double twoPointFieldGoalsAttempted;
private double twoPointFieldGoalsMade;
private double threePointersAttempted;
private double threePointersMade;
private int turnovers;
private int assist;
private String stats;
public BasketBallPlayer(String name, int height, int weight, double freeThrowsMade, double twoPointFieldGoalsAttempted,double twoPointFieldGoalsMade, double threePointersAttempted, double threePointersMade, int assist, int turnovers)
{
//identifies the age, name, height-in., weight-lbs
this.name=name;
this.height=height;
this.weight=weight;
this.freeThrowsAttempted=freeThrowsAttempted;
this.freeThrowsMade=freeThrowsMade;
this.twoPointFieldGoalsAttempted=twoPointFieldGoalsAttempted;
this.threePointersMade=threePointersMade;
this.turnovers=turnovers;
this.assist=assist;
}
public BasketBallPlayer(int weight, int height, String name)
//identifies the weight(lbs.), height(inches) and String name
{
//identifies the name, height-in., weight-lbs
this.name=name;
this.height=height;
this.weight=weight;
}
//Sets the Name
public void setName(String name)
{
this.name=name;
}
//Sets the Height
public void setHeight (int height)
{
this.height=height;
}
//Sets the Weight
public void setWeight (int weight)
{
this.weight=weight;
}
//Sets the Free Throws Attempted
public void setFreeThrowsAttempted( double freeThrowsAttempted)
{
this.freeThrowsAttempted=freeThrowsAttempted;
}
//Sets the Free Throws Made
public void setFreeThrowsMade(double freeThrowsMade)
{
this.freeThrowsMade=freeThrowsMade;
}
// Sets two Point Field Goals Attempted
public void setTwoPointFieldGoalsAttempted (double twoPointFieldGoalsAttempted)
{
this.twoPointFieldGoalsAttempted=twoPointFieldGoalsAttempted;
}
public void setTwoPointFieldGoalsMade (double twoPointFieldGoalsMade)
{
this.twoPointFieldGoalsMade=twoPointFieldGoalsMade;
}
public void setThreePointerAttempted(double threePointersAttempted)
{
this.threePointersAttempted=threePointersAttempted;
}
public void setThreePointersMade(double threePointersMade)
{
this.threePointersMade=threePointersMade;
}
public void setTurnovers(int turnovers)
{
this.turnovers=turnovers;
}
public void setAssist(int assist)
{
this.assist=assist;
}
//Returns a Name
public String getName()
{
return name;
}
public int getHeight ()
{
return height;
}
public int getWeight ()
{
return weight;
}
public double getFreeThrowsAttempted()
{
return freeThrowsAttempted;
}
public double getfreeThrowsMade()
{
return freeThrowsMade;
}
public double getTwoPointFieldGoalsAttempted ()
{
return twoPointFieldGoalsAttempted;
}
public double getTwoPointFieldGoalsMade ()
{
return twoPointFieldGoalsMade;
}
public double getThreePointerAttempted()
{
return threePointersAttempted;
}
public double getthreePointersMade()
{
return threePointersMade;
}
public int getTurnovers()
{
return turnovers;
}
public int gettAssist()
{
return assist;
}
/** The geStats Method allows you to get all information on the player in print. All Percentages
*
*/
public void getStats()
{
double Percentage1;
double Percentage2;
double Percentage3;
Percentage1=(double)((twoPointFieldGoalsMade*100)/twoPointFieldGoalsAttempted);
Percentage2=((double)(threePointersMade*100)/threePointersAttempted);
Percentage3=((double)((freeThrowsMade*100)/freeThrowsAttempted));
System.out.println("*************************");
System.out.println("BasketBall Player Name:" + name);
System.out.println("Field Goal Percentage:" + Percentage1 +"%");
System.out.println("3 Pointer Percentage:" + Percentage2 +"%");
System.out.println("Free Throw Percentage:" + Percentage3 +"%");
System.out.println("Assist to Turnover Ration:" + assist/turnovers);
System.out.println("*************************");
}
}
First, you should not be using doubles for these number. It just accounts to inefficiency and makes no sense. However, when using ints, you have to note that you cannot directly get a percentage when dividing two ints. This piece of code should work, you may want to include some code that checks that the divisors are not 0.
private int freeThrowsAttempted;
private int freeThrowsMade;
private int twoPointFieldGoalsAttempted;
private int twoPointFieldGoalsMade;
private int threePointersAttempted;
private int threePointersMade;
...
double percentage1 = (twoPointFieldGoalsMade * 100F) / twoPointFieldGoalsAttempted;
double percentage2 = (threePointersMade * 100F) / threePointersAttempted;
double percentage3 = (freeThrowsMade * 100F) / freeThrowsAttempted;
When I run javac Hero.java Then java Hero The program spits out tons of lines that look like this:
Hero#322ba3e4
Hero#4f14e777
Hero#65685e30
Hero#26ffd553
Hero#660e5025
Hero#35afe17b
What is this? Hero#322ba3e4
Then it shows,
Exception in thread "main" java.lang.StackOverflowError
at sun.nio.cs.UTF_8.updatePositions(UTF_8.java:77)
at sun.nio.cs.UTF_8$Encoder.encodeArrayLoop(UTF_8.java:564)
at sun.nio.cs.UTF_8$Encoder.encodeLoop(UTF_8.java:619)
at java.nio.charset.CharsetEncoder.encode(CharsetEncoder.java:561)
at sun.nio.cs.StreamEncoder.implWrite(StreamEncoder.java:271)
at sun.nio.cs.StreamEncoder.write(StreamEncoder.java:125)
at java.io.OutputStreamWriter.write(OutputStreamWriter.java:207)
at java.io.BufferedWriter.flushBuffer(BufferedWriter.java:129)
at java.io.PrintStream.write(PrintStream.java:526)
at java.io.PrintStream.print(PrintStream.java:669)
at java.io.PrintStream.println(PrintStream.java:823)
at Hero.DrowRanger(Hero.java:78)
at Hero.DrowRanger(Hero.java:79)
This line repeats 100s of times:
at Hero.DrowRanger(Hero.java:79)
This is the method at that line,
private static Object DrowRanger() {
Hero DrowRanger = new Hero(
0,
"Agility",
"Ranged",
"Frost Arrows",
"Gust",
"Precision Aura",
"Marksmanship",
17,
1.9,
26,
1.9,
15,
1.4,
473,
195,
44,
55,
625,
0.64,
300);
System.out.println(DrowRanger);
return DrowRanger(); // Line 79 Is Here
}
This is the full class
public class Hero implements NPC {
private int level;
private String primaryAttribute;
private String attackType;
private String ability1;
private String ability2;
private String ability3;
private String ability4;
private double strength;
private double strengthMultiplier;
private double agility;
private double agilityMultiplier;
private double intelligence;
private double intelligenceMultiplier;
private int health;
private int mana;
private int damageMin;
private int damageMax;
private int range;
private double armor;
private int movement;
//default constructor
public Hero(
int level,
String primaryAttribute,
String attackType,
String ability1,
String ability2,
String ability3,
String ability4,
double strength,
double strengthMultiplier,
double agility,
double agilityMultiplier,
double intelligence,
double intelligenceMultiplier,
int health,
int mana,
int damageMin,
int damageMax,
int range,
double armor,
int movement
) {
} // End Constructor
public static void main (String[] args) {
DrowRanger();
}
private static Object DrowRanger() {
Hero DrowRanger = new Hero(
0,
"Agility",
"Ranged",
"Frost Arrows",
"Gust",
"Precision Aura",
"Marksmanship",
17,
1.9,
26,
1.9,
15,
1.4,
473,
195,
44,
55,
625,
0.64,
300);
System.out.println(DrowRanger);
return DrowRanger();
}
// getters and setters - required to implement ALL from interface
public int getLevel() {
return this.level;
}
public String getPrimaryAttribute() {
return this.primaryAttribute;
}
public String getAttackType() {
return this.attackType;
}
public String getAbility1() {
return this.ability1;
}
public String getAbility2() {
return this.ability2;
}
public String getAbility3() {
return this.ability3;
}
public String getAbility4() {
return this.ability4;
}
public double getStrength() {
return this.strength;
}
public double getStrengthMultiplier() {
return this.strengthMultiplier;
}
public double getAgility() {
return this.agility;
}
public double getAgilityMultiplier() {
return this.agilityMultiplier;
}
public double getIntelligence() {
return this.intelligence;
}
public double getIntelligenceMultiplier() {
return this.intelligenceMultiplier;
}
public int getHealth() {
return this.health;
}
public int getMana() {
return this.mana;
}
public int getDamageMin() {
return this.damageMin;
}
public int getDamageMax() {
return this.damageMax;
}
public int getRange() {
return this.range;
}
public double getArmor() {
return this.armor;
}
public int getMovement() {
return this.movement;
}
// This is where the setters are.
public void setLevel(int level) {
this.level = level;
}
public void setPrimaryAttribute(String primaryAttribute) {
this.primaryAttribute = primaryAttribute;
}
public void setAttackType(String attackType) {
this.attackType = attackType;
}
public void setAbility1(String ability1) {
this.ability1 = ability1;
}
public void setAbility2(String ability2) {
this.ability2 = ability2;
}
public void setAbility3String(String ability3) {
this.ability3 = ability3;
}
public void setAbility4(String ability4) {
this.ability4 = ability4;
}
public void setStrength(double strength) {
this.strength = strength;
}
public void setStrengthMultiplier(double strengthMultiplier) {
this.strengthMultiplier = strengthMultiplier;
}
public void setAgility(double agility) {
this.agility = agility;
}
public void setAgilityMultiplier(double agilityMultiplier) {
this.agilityMultiplier = agilityMultiplier;
}
public void setIntelligence(double intelligence) {
this.intelligence = intelligence;
}
public void setIntelligenceMultiplier(double intelligenceMultiplier) {
this.intelligenceMultiplier = intelligenceMultiplier;
}
public void setHealth(int health) {
this.health = health;
}
public void setMana(int mana) {
this.mana = mana;
}
public void setDamageMin(int damageMin) {
this.damageMin = damageMin;
}
public void setDamageMax(int damageMax) {
this.damageMax = damageMax;
}
public void setRange(int range) {
this.range = range;
}
public void setArmor(double armor) {
this.armor = armor;
}
public void setMovement(int movement) {
this.movement = movement;
}
} // End Character Class
`
Your DrowRanger method is calling itself infinitely. This is almost always the cause of a StackOverflowException.
This line:
return DrowRanger();
Calls the DrowRanger method, but since you're inside the DrowRanger method it just keeps calling itself infinitely. I think you meant to return the local object:
return DrowRanger;
In general, it is a bad idea to give methods and objects the same name. Also, it is java convention always start method and variable/field names with a lowercase letter.
The output you're seeing is how java prints objects that have not overridden the toString() method. See this post for an explanation of how the toString() method works.
To the first: println prints the output of the toString method of the object. As it is just the normal method inherited by the Object class it just returns the name of the class and the hash of the object.
To your error: it looks like there is an infinite loop somewhere (which has been found).
I am using following query
String queryString = "select NEW com.h.offering.dto.SellerDetailsDto(p.productId,p.sellerId, p.sellerSku, p.sellPrice, " +
"p.transferPrice, p.sellerMRP,p.aCommission,p.baseShippingFee,p.addnShippingFee, " +
"p.propogationLevel,p.propogationValue,a.warehouseName,a.quantity,a.maxShippingTime,a.minShippingTime) "
+ "from PriceDetails p, AvailabilityDetails a "
+ "where a.productId = p.productId "
+ "and a.sellerSku = p.sellerSku "
+ "and a.sellerId = :sellerId";
while executing i am getting error
org.hibernate.hql.ast.QuerySyntaxException: Unable to locate appropriate constructor on class [com.a.offering.dto.SellerDetailsDto] [select NEW com.s.offering.dto.SellerDetailsDto(p.productId) from com.a.offering.db.domain.PriceDetails p, com.a.offering.db.domain.AvailabilityDetails a where a.productId = p.productId and a.sellerSku = p.sellerSku and a.sellerId = :sellerId]
at org.hibernate.hql.ast.QuerySyntaxException.convert(QuerySyntaxException.java:54)
at org.hibernate.hql.ast.QuerySyntaxException.convert(QuerySyntaxException.java:47)
at org.hibernate.hql.ast.ErrorCounter.throwQueryException(ErrorCounter.java:82)
at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:261)
at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:185)
at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:136)
and so on...
I am unable to figure out the error. Help me out
The code for SellerDetailsDto.java is
package com.a.offering.dto;
public class SellerDetailsDto {
public String productId;
public String sellerId;
public String sellerSku;
public Double sellPrice;
public Double transferPrice;
public Double sellerMRP;
public Double a;
public Double baseShippingFee;
public Double addnShippingFee;
public String propogationLevel;
public String propogationValue;
public String warehouseName;
public int quantity;
public int maxShippingTime;
public int minShippingTime;
public String getProductId() {
return productId;
}
public void setProductId(String productId) {
this.productId = productId;
}
public String getSellerId() {
return sellerId;
}
public void setSellerId(String sellerId) {
this.sellerId = sellerId;
}
public String getSellerSku() {
return sellerSku;
}
public void setSellerSku(String sellerSku) {
this.sellerSku = sellerSku;
}
public String getWarehouseName() {
return warehouseName;
}
public void setWarehouseName(String warehouseName) {
this.warehouseName = warehouseName;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
public int getMaxShippingTime() {
return maxShippingTime;
}
public void setMaxShippingTime(int maxShippingTime) {
this.maxShippingTime = maxShippingTime;
}
public int getMinShippingTime() {
return minShippingTime;
}
public void setMinShippingTime(int minShippingTime) {
this.minShippingTime = minShippingTime;
}
public Double getSellPrice() {
return sellPrice;
}
public void setSellPrice(Double sellPrice) {
this.sellPrice = sellPrice;
}
public Double getTransferPrice() {
return transferPrice;
}
public void setTransferPrice(Double transferPrice) {
this.transferPrice = transferPrice;
}
public Double getSellerMRP() {
return sellerMRP;
}
public void setSellerMRP(Double sellerMRP) {
this.sellerMRP = sellerMRP;
}
public Double a() {
return a;
}
public void a(Double aa) {
a }
public Double getBaseShippingFee() {
return baseShippingFee;
}
public void setBaseShippingFee(Double baseShippingFee) {
this.baseShippingFee = baseShippingFee;
}
public Double getAddnShippingFee() {
return addnShippingFee;
}
public void setAddnShippingFee(Double addnShippingFee) {
this.addnShippingFee = addnShippingFee;
}
public String getPropogationLevel() {
return propogationLevel;
}
public void setPropogationLevel(String propogationLevel) {
this.propogationLevel = propogationLevel;
}
public String getPropogationValue() {
return propogationValue;
}
public void setPropogationValue(String propogationValue) {
this.propogationValue = propogationValue;
}
}
Hibernate needs a constructor with at least package private (i. e. default) visibility.
Normally a parameterless constructor is necessary (which you implicitly have), but for your select new ... SellerDetailsDto needs a constructor with the 15 parameters which you give in the select statement. (Thought the error message demands a constructor with only the id as a parameter - it looks like if the error is coming from a different select new statement.) You don't have such a constructor.
The problem is that you don't have a constructor in SellerDetailsDto that accepts the parameters that you are passing in the hql query. Add this constructor to your class:
public SellerDetailsDto(String productId, String sellerId,
String sellerSku, Double sellPrice, Double transferPrice,
Double sellerMRP, Double tradusCommission, Double baseShippingFee,
Double addnShippingFee, String propogationLevel,
String propogationValue, String warehouseName, int quantity,
int maxShippingTime, int minShippingTime) {
this.productId = productId;
this.sellerId = sellerId;
this.sellerSku = sellerSku;
this.sellPrice = sellPrice;
this.transferPrice = transferPrice;
this.sellerMRP = sellerMRP;
this.tradusCommission = tradusCommission;
this.baseShippingFee = baseShippingFee;
this.addnShippingFee = addnShippingFee;
this.propogationLevel = propogationLevel;
this.propogationValue = propogationValue;
this.warehouseName = warehouseName;
this.quantity = quantity;
this.maxShippingTime = maxShippingTime;
this.minShippingTime = minShippingTime;
}
This could make some other code you have to stop working, because now there wont' be a default constructor available. To fix that, add a default constructor too:
public SellerDetailsDto() {}
Actually this is problem on your root pom file. You want install lombok in your IDE & add dependency of lombok in root pom file I hope it will work