classes are the fundamental building blocks in Java. They store information about an object and the actions to modify that information.
Submit each of the following practice assignments at codePost.io.
To register for a free codePost account, please follow these instructions.
Watch this short video for a demonstration of submitting an assignment and reviewing the results:
Note, currently, feedback is not visible in Safari.
Assume that we want to have a class to store data and actions relating to a robot.
For this assignment, you get to do just the very first step.
Just write code to create the class (you can leave the entire body of the class blank :). Name the class Robot (and therefore, save it in a file named Robot.java). Submit your Robot.java to the "Writing Classes Syntax" assignment on codePost.
Note, do not include a constructor with parameters.
Write a class to store data and actions relating to a book. For now, just model a book by it's title, author and number of pages. Create three (private) fields in your class to store this information. Name the class Book. Additionally, provide the following methods:
codePost Test] setters Test:17: error: cannot find symbol littleEngineObj.setTitle("The Little Engine That Could"); ^ symbol: method setTitle(String) location: variable littleEngineObj of type BookIn this case, it cannot find the "setTitle" method in the Book class that was submitted. If you have a method with all lowercase letters (i.e., settitle), that is a different method than setTitle and is the reason for the "cannot find symbol" compilation error.
Write a CarForSale class so that the CarForSaleDriver class compiles and executes as illustrated in the examples. Do not modify the CarForSaleDriver class. Upload just your CarForSale.java file to codePost.
CarForSaleDriver.java:import java.util.Scanner; /** * Driver code for the CarForSale class * @author Hyrum D. Carroll * @version 0.2 (March 21, 2022) */ public class CarForSaleDriver{ public static void main( String[] args ){ // Prompt the user for the model, asking price and year for two different vehicles Scanner stdin = new Scanner( System.in ); // local variables to hold input from the user for each of the two vehicles String modelFromUser = ""; double priceFromUser = 0.0; int yearFromUser = 0; System.out.print( "Please enter the model of the first vehicle: "); modelFromUser = stdin.next(); System.out.println( "You entered " + modelFromUser); System.out.print( "Please enter the asking price for the " + modelFromUser + ": "); priceFromUser = stdin.nextDouble(); System.out.println( "You entered " + priceFromUser); System.out.print( "Please enter the year of the " + modelFromUser + ": "); yearFromUser = stdin.nextInt(); System.out.println( "You entered " + yearFromUser); // first vehicle CarForSale carA = new CarForSale( modelFromUser, priceFromUser, yearFromUser); // second vehicle CarForSale carB = new CarForSale(); System.out.print( "Please enter the model of the second vehicle: "); modelFromUser = stdin.next(); System.out.println( "You entered " + modelFromUser); carB.setModel( modelFromUser ); System.out.print( "Please enter the asking price for the " + carB.getModel() + ": "); priceFromUser = stdin.nextDouble(); System.out.println( "You entered " + priceFromUser); carB.setPrice( priceFromUser ); System.out.print( "Please enter the year of the " + carB.getModel() + ": "); yearFromUser = stdin.nextInt(); System.out.println( "You entered " + yearFromUser); carB.setYear( yearFromUser ); System.out.println(); // Display the car to by based on the asking price then the year boolean decided = false; if( carA.getPrice() < carB.getPrice() ){ System.out.println( "The " + carA.getModel() + " has a lower asking price" ); if( carA.getYear() >= carB.getYear() ){ System.out.println( "The " + carA.getModel() + " is also newer" ); System.out.println( "I'm going to go with the " + carA.toString() ); decided = true; } }else{ System.out.println( "The " + carB.getModel() + " has a lower asking price" ); if( carB.getYear() >= carA.getYear() ){ System.out.println( "The " + carB.getModel() + " is also newer" ); System.out.println( "I'm going to go with the " + carB.toString() ); decided = true; } } // Neither vehicle is an obvious choice, so display both of them if( ! decided ){ System.out.println("I can't decided between the following cars:"); System.out.println( carA.toString() ); System.out.println( carB.toString() ); } } }
Please enter the model of the first vehicle: Avalon You entered Avalon Please enter the asking price for the Avalon: 4995.0 You entered 4995.0 Please enter the year of the Avalon: 2006 You entered 2006 Please enter the model of the second vehicle: xB You entered xB Please enter the asking price for the xB: 4495.0 You entered 4495.0 Please enter the year of the xB: 2005 You entered 2005 The xB has a lower asking price I can't decided between the following cars: 2006 Avalon with an asking price of $4995.0 2005 xB with an asking price of $4495.0
Please enter the model of the first vehicle: Transit You entered Transit Please enter the asking price for the Transit: 31950.5 You entered 31950.5 Please enter the year of the Transit: 2018 You entered 2018 Please enter the model of the second vehicle: Express You entered Express Please enter the asking price for the Express: 29999.99 You entered 29999.99 Please enter the year of the Express: 2018 You entered 2018 The Express has a lower asking price The Express is also newer I'm going to go with the 2018 Express with an asking price of $29999.99
CarForSaleDriver.java:70: error: constructor CarForSale in class CarForSale cannot be applied to given types; CarForSale carB = new CarForSale(); ^ required: String,double,int found: no arguments reason: actual and formal argument lists differ in length 1 errorHere, "required: String,double,int" means that the submitted code (your code) has a method (in this case the constructor) that requires 3 arguments (i.e., a String, a double and int), but only a constructor with "no arguments" was "found" in your code. Notice, that the CarForSaleDriver code requires both a constructor with 3 arguments and a constructor without any arguments.
CarForSale@33909752This is the result of calling the toString method, which is then passed to the println method. You need to write a method named toString that returns a String that matches the pattern in the examples.