Question

I am having a problem with my code. Here are the relevant parts. The method priceNow...

I am having a problem with my code. Here are the relevant parts.

The method priceNow is supposed to take as an input a string in the form "Q1'20" (for example) and calculate the new price knowing that the price of the CPU decreases by 2% every quarter.

Here is my class definition code:

public class CPUv2 {
   private String launchDate;
   public CPUv2() {
       this.CPUGeneration = 1;
       this.CPUSeries = "i3";
       this.suggestedPrice = 117;
       this.CPUSpeed = 2.93;
       this.launchDate = "Q1’10";
       this.SGX = false;
   }
   public double priceNow(String sQuarterYear) {
  
       int currQuarter = Integer.parseInt(sQuarterYear.charAt(1) + "");
   int launchQuarter = Integer.parseInt(launchDate.charAt(1) + "");
       int launchYear = Integer.parseInt(launchDate.split("'")[1]);
   int currYear = Integer.parseInt(sQuarterYear.split("'")[1])
   int totalQuarter;
   if (currYear > launchYear) {

   totalQuarter = 4 * (currYear - launchYear) + (currQuarter - launchQuarter);
   } else if (currYear < launchYear) {

   totalQuarter = 0;
   } else {

   totalQuarter = currQuarter - launchYear;
   }
   double price = suggestedPrice - suggestedPrice * totalQuarter * .02;
   //applying the condition that the depriciated price cant be less than 0
   if (price>=0) {
         
   return (price);
   }
   else {
       return 0;
   }
   }
  

}

here is my driver (tester) code:

public class CPUv2Testing {
  
   public static void main(String[] args) {
       CPUv2 cpu1 = new CPUv2();
       System.out.println(cpu1.priceNow("Q3'20"));
      
}

}

the error i am getting is when i call the priceNow method in the class, but i cant figure out why.

Any help would be appreciated!

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The answer to this question is as follows:

The modified code is as follows:

public class CPUv2 {
private String launchDate;
//The modified code is as follows
private int CPUGeneration;
private String CPUSeries;
private int suggestedPrice;
private double CPUSpeed;

  
private boolean SGX;
public CPUv2() {
//Here if you use the keyword this then u need to declare the variable along with its type
this.CPUGeneration = 1;
this.CPUSeries = "i3";
this.suggestedPrice = 117;
this.CPUSpeed = 2.93;
this.launchDate = "Q1'10";
this.SGX = false;
}
public double priceNow(String sQuarterYear) {
  
int currQuarter = Integer.parseInt(sQuarterYear.charAt(1) + "");
int launchQuarter = Integer.parseInt(launchDate.charAt(1) + "");
int launchYear = Integer.parseInt(launchDate.split("'")[1]);
//Here you missed semicolon for the below statement
int currYear = Integer.parseInt(sQuarterYear.split("'")[1]);
int totalQuarter;
if (currYear > launchYear) {

totalQuarter = 4 * (currYear - launchYear) + (currQuarter - launchQuarter);
} else if (currYear < launchYear) {

totalQuarter = 0;
} else {

totalQuarter = currQuarter - launchYear;
}
double price = suggestedPrice - suggestedPrice * totalQuarter * .02;
//applying the condition that the depriciated price cant be less than 0
if (price>=0) {

return (price);
}
else {
return 0;
}
}
  

}

//here is my driver (tester) code:

public class CPUv2Testing {
  
public static void main(String[] args) {
CPUv2 cpu1 = new CPUv2();
System.out.println(cpu1.priceNow("Q3'20"));
  
}

}

The output screen is as follows:

Add a comment
Know the answer?
Add Answer to:
I am having a problem with my code. Here are the relevant parts. The method priceNow...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • i need help fixing my code. here is the assignment: Make a class that contains the...

    i need help fixing my code. here is the assignment: Make a class that contains the following functions: isLong, isDouble, stringToLong, and stringToDouble. Each receives a string and returns the appropriate type (bool, bool, long, and double).During the rest of the semester you will paste this class into your programs and will no longer use any of the "standard" c# functions to convert strings to numbers. here is my code. it works for the long method but not the double:...

  • I am trying to run a couple of functions in my main and It won't output...

    I am trying to run a couple of functions in my main and It won't output anything if there is anything that you see wrong please help me out. I have to use simple, sometimes slower ways because we can use what we haven't learned yet. Here is my code. package a3; import java.util.Scanner; public class LoopsAndImages { public static void main(String[] args){ String test = "A rabbit has a carrot"; Boolean numbers = hasMoreEvenThanOdd("12344"); System.out.println(test); System.out.println(hideLetterA(test)+" This is the...

  • Need Help finishing up MyCalendar Program: Here are the methods: Modifier and Type Method Desc...

    Need Help finishing up MyCalendar Program: Here are the methods: Modifier and Type Method Description boolean add​(Event evt) Add an event to the calendar Event get​(int i) Fetch the ith Event added to the calendar Event get​(java.lang.String name) Fetch the first Event in the calendar whose eventName is equal to the given name java.util.ArrayList<Event> list() The list of all Events in the order that they were inserted into the calendar int size() The number of events in the calendar java.util.ArrayList<Event>...

  • Good evening, I am having issue removing every positive multiple of five from my code. Here...

    Good evening, I am having issue removing every positive multiple of five from my code. Here are the instructions :Output the numbers 1-1000, but make all numbers that are a multiple of five negative. Output a new line after each number. Here is my code: public class Example3 { public static void main(String[] args) { for (int counter = 1 ; counter <= 1000; counter++){ System.out.println(counter); if (counter%5==0) { System.out.println(counter*-1); } } } } _________________________________________________________ my output is good until...

  • Im writing a method to evaluate a postfix expression. Using my own stack class. Here is my code but I keep getting a classcastexception where it says java.lang.Character cannot be cast to java.lang,In...

    Im writing a method to evaluate a postfix expression. Using my own stack class. Here is my code but I keep getting a classcastexception where it says java.lang.Character cannot be cast to java.lang,Integer. Im not sure how to fix this. public class Evaluator { public static void evaluatePost(String postFix)    {        LinkedStack stack2 = new LinkedStack();        int val1;        int val2;        int result;        for(int i = 0; i < postFix.length(); i++)        {            char m = postFix.charAt(i);            if(Character.isDigit(m))            {                stack2.push(m);            }            else            {               ...

  • Hi, I am writing Java code and I am having a trouble working it out. The...

    Hi, I am writing Java code and I am having a trouble working it out. The instructions can be found below, and the code. Thanks. Instructions Here are the methods needed for CIS425_Student: Constructor: public CIS425_Student( String id, String name, int num_exams ) Create an int array exams[num_exams] which will hold all exam grades for a student Save num_exams for later error checking public boolean addGrade( int exam, int grade ) Save a grade in the exams[ ] array at...

  • /** this is my code, and I want to invoke the method, doubleArraySize() in main method....

    /** this is my code, and I want to invoke the method, doubleArraySize() in main method. and I need to display some result in cmd or terminal. also, I have classes such as Average and RandomN needed for piping(pipe). please help me and thank you. **/ import java.util.Scanner; public class StdDev { static double[] arr; static int N; public static void main(String[] args) { if(args.length==0){ arr= new double[N]; Scanner input = new Scanner(System.in); int i=0; while(input.hasNextDouble()){ arr[i] = i++; }...

  • How to build Java test class? I am supposed to create both a recipe class, and...

    How to build Java test class? I am supposed to create both a recipe class, and then a class tester to test the recipe class. Below is what I have for the recipe class, but I have no idea what/or how I am supposed to go about creating the test class. Am I supposed to somehow call the recipe class within the test class? if so, how? Thanks in advance! This is my recipe class: package steppingstone5_recipe; /** * *...

  • this is for java programming Please Use Comments I am not 100% sure if my code...

    this is for java programming Please Use Comments I am not 100% sure if my code needs work, this code is for the geometric if you feel like you need to edit it please do :) Problem Description: Modify the GeometricObject class to implement the Comparable interface and define a static max method in the GeometricObject class for finding the larger (in area) of two GeometricObject objects. Draw the UML and implement the new GeometricObject class and its two subclasses...

  • I am creating a program where a user will enter in a price amount. And then...

    I am creating a program where a user will enter in a price amount. And then the program will determine how many 20, 10, 5, 1, etc.. they get back. I keep getting "error: unexpected type" in my findValue method. Not too sure why I am getting this. There also might be more bugs in there that I am not aware of because it wont compile. Any help is appreciated. Here is my code: import java.util.Scanner; public class prac1 {...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT