I have a question about my inventory program that needs a little tweaking to be functional. Essentially, I am asking the using to input the amount of a product that the individual wants to order, I want it to save in RAM, and then output the number of products and the total cost for the user. Here is my first constructor class "Item".
import java.text.*;
import java.util.*;
import javax.swing.*;
public class Item
{
public static final double SUBWOOFER_VALUE= 175.99;
public static final double TWEETER_VALUE= 34.49;
public int subOrder;
public int tweetOrder;
public double totPrice;
public int subStock;
public int tweetStock;
public Item()
{
int subStock=1000;
int tweetStock=3000;
double subPrice= (subOrder*SUBWOOFER_VALUE);
double tweetPrice= (tweetOrder*TWEETER_VALUE);
}
public void addSubOrder(int newsubOrder)
{
subOrder=newsubOrder;
}
public int getSubOrder()
{
return subOrder;
}
public void addTweetOrder(int newtweetOrder)
{
tweetOrder= newtweetOrder;
}
public int getTweetOrder()
{
return tweetOrder;
}
public void newSubStock(int newSubStock)
{
subStock= newSubStock;
newSubStock= (subStock)-(subOrder);
}
public int getsubStock()
{
return subStock;
}
public void newTweetStock(int newTweetStock)
{
tweetStock= newTweetStock;
newTweetStock= (tweetStock)-(tweetOrder);
}
public int gettweetStock()
{
return tweetStock;
}
public void totPrice(double newTotPrice, double subPrice, double tweetPrice)
{
newTotPrice= (subPrice+tweetPrice)*(.065);
totPrice= newTotPrice;
}
public double getTotPrice()
{
return totPrice;
}
public static void main (String[] args)
{
Item Order= new Item();
}//end main
}
And then here is my controller class, that controls the inputs and the outputs...
import javax.swing.*;
import java.util.*;
public class Controller
{
public static void main (String[] args)
{
Scanner myScan=new Scanner(System.in);
Item Order= new Item();
String question= "Would you like to make an order?";
String question1= "Would you like to order Subwoofers?";
String question2= "Would you like to order Tweeters?";
String question3= "How many would you like to order?";
String question4="Would you like to place another order?";
String thank= "Thank you for your order,";
System.out.println(question);
String answer= myScan.nextLine();
System.out.println(question1);
while(!answer.equalsIgnoreCase("No"))
{
String subAns= myScan.nextLine();
if(subAns.equals("Yes"))
{
System.out.println(question3);
int subOrder= myScan.nextInt();
}
System.out.println(question2);
String tweetAns= myScan.nextLine();
if(tweetAns.equals("Yes"))
{
System.out.println(question3);
int tweetOrder= myScan.nextInt();
}
else//If subAns= no then proceed to ask about tweeters.
{
tweetAns= myScan.nextLine();
if(tweetAns.equals("Yes"))//If tweeter answer = yes then proceed to ask for an amount
{
System.out.println(question3);
int tweetOrder= myScan.nextInt();
}
}break;
}//end while
System.out.println(thank);
System.out.println("Your ordered: "+ Order.getSubOrder() + " Subwoofer's and "+ Order.getTweetOrder() + " Tweeter's");
System.out.println("Your total is:"+ Order.getTotPrice());
}//end main
}//Two items
So when I input something and expect an output, I know get 0's in my output.
----jGRASP exec: java Controller
Would you like to make an order?
Yes
Would you like to order Subwoofers?
Yes
How many would you like to order?
9
Would you like to order Tweeters?
Yes
How many would you like to order?
4
Thank you for your order,
Your ordered: 0 Subwoofer's and 0 Tweeter's
Your total is:0.0
----jGRASP: operation complete.
Any help would be greatly appreciated.
you never call any of your methods to calculate update the state of Item (totPrice()) and you don't update the fields yourself ( subStock tweetStock) thus they are still their initial value because you never changed them.
On a side note it would make more sense for you to make a subwoofer and tweeter object of type Item in your controller to avoid having to double all your code in item
Also since java is pass by value, assigning to your functions arguments (such asnewSubStock= (subStock)-(subOrder); in newSubStock()) are redundant and have no purpose
I wonder why are you using swing.* import? You need to rectify your code a lot. You are using ignore case for 'NO' but not using the same for 'yes'. You have never used 'question4'. And also once you get the order in subOrder and tweetOrder, where are you using it? You need to call the methods of Item object in order to do the calculations.
Try adding "Order.addSubOrder(subOrder); & Order.addTweetOrder(tweetOrder);" after getting the orders.
Related
I have written a code in java-
public class school
{
int rollno;
public void setRollno(int r)
{
rollno = r;
}
public int getRollno(int r)
return roll no;
}
}
class enca
{
Public static void main (String []args)
{
School sc = new School();
sc.setRollno();
System.out.println(sc.getRollno());
}
}
A Java program to get roll from user and there it is giving an error while compiling the code as marking to School object
There is error in School object ...
So how do I solve it?
I see multiple errors in the current code given.
your class name is "school" but then when creating the sc object you used "School"
getRollno is missing its opening flower bracket and also returns "roll no" instead of "rollno" and is also accepting input for some reason
in the main method, you said sc.setRollno() without inputting any arguments, even though it is expecting a rollno
for public static void main, you maid your public capitalized as "Public" instead of "public"
also, I recommend using tabs when creating your code because it makes it readable for not just yourself but also for others reading your code
agree with #slayerofthend
and this is the answer u need maybe. u can compare with urs.
public class Enca {
public static void main(String[] args) {
School sc = new School();
sc.setRollno(1);
System.out.println(sc.getRollno());
}
public static class School {
int rollno;
public void setRollno(int r) {
rollno = r;
}
public int getRollno() {
return rollno;
}
}
}
I am writing a program for which students going to the cashier of particular department or faculty are given a token number which is automatically generated and they line up in a queue to wait for their turn to enter. The program should allow the user to insert a new student in the queue by adding his name and token number into the system among its other features.
I don't know how to add a new student to the queue.
Here is what I have come up so far:
Student class
package queues;
import java.util.Random;
public class Student {
private String name;
private int tnum;
public Student(String name, int tnum){
this.name=name;
this.tnum=tnum;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getTnum() {
return tnum;
}
public void setTnum(int tnum) {
this.tnum = tnum;
}
public String toString(){
return "Student name: "+ name+ " Token num: "+tnum;
}
}
Main class
package queues;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Random;
import java.util.Scanner;
public class Student_Main{
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
int opt;
Student stdt= new Student("Sophia", 1);
Student stdt2= new Student("Amelia", 2);
Student stdt3= new Student("Karxlina", 4);
Student stdt4= new Student("Rachel", 3);
Queue<Student> stdtQ= new LinkedList<Student>();
stdtQ.add(stdt);
stdtQ.add(stdt2);
stdtQ.add(stdt3);
stdtQ.add(stdt4);
System.out.println(stdtQ);
System.out.println("Please choose an option. ");
System.out.println("To insert new student, enter 1.");
opt= sc.nextInt();
if(opt==1){
stdtQ.add(Student(sc.hasNext(), sc.nextInt())); /*this doesn't work of course*/
}
}
}
You should write:
stdtQ.add(new Student(sc.hasNext(), sc.nextInt()));
Queues typically order elements in a FIFO manner. So if you are interested in using the token number to sort the queue you should check priority queues.
By calling the add method you are already adding new elements to the stdtQ queue. If you want toadd other students to that queue you fist have to create the object ( by getting the values from the input for example) and the add it with the add method.
You can print all the student in the queue like this:
for(Student s : stdtQ){
System.out.println(s.getName() + s.getTnum());
}
I've created a simplified version of my current assignment for my programming course.
I've created a test program that asks the user for an input, studentName, studentName is then validated through another class. My end goal is to print out a method called toString() that holds the value the user has entered for the studentName. Right now my program returns null, not the value of studentName. The problem is I'm not sure how to properly set the values with a constructor.
If you could set up a proper constructor and a way to properly print the value the user has entered through the command prompt, I would appreciate it!
Here is the class that contains the main method. Note: I may have declared too many class objects because I was in a hurry.
import java.util.Scanner;
public class TestMcTest
{
TestMcTest2 test3 = new TestMcTest2();
public static void main(String[] args)
{
TestMcTest test2 = new TestMcTest();
TestMcTest2 test = new TestMcTest2();
test2.getStudentInfo();
System.out.println(test.toString());
}
public void getStudentInfo()
{
int valid = 0;
Scanner input = new Scanner(System.in);
do
{
System.out.println("Enter a name for a student");
valid = test3.getStudentName(input.nextLine());
}while(valid == 0);
}
}
Here is the class that holds the validation and the toString() method that I want to call into the main method of the class with the main method.
public class TestMcTest2
{
private String studentName;
public String setStudentName()
{
return studentName;
}
public int getStudentName(String studentName)
{
int valid = 0;
if (studentName.length() != 0)
{
valid = 1;
this.studentName = studentName;
}
return valid;
}
public String toString()
{
return this.studentName;
}
}
You dint set the value of variable studentName so the default value of string is printed i.e. null
In TestMcTest you have do
TestMcTest2 test = new TestMcTest2();
test.setStudentName("singhakash");
System.out.println(test.toString());
and in TestMcTest2 change
public void setStudentName(String studentName){
this.studentName = studentName;
}
this will give the expected output.
Btw your method name says opposite of its functionality.
If you could set up a proper constructor and a way to properly print
the value the user has entered through the command prompt, I would
appreciate it!
public class TestMcTest2{
....
public TestMcTest2(String s){
this.studentName = s;
}
}
You have to use the newly constructor now like this:
TestMcTest2 test3 = new TestMcTest2("hot name");
And you can print it out like this after your do-while
System.out.println(test3.toString());
And I'm pretty sure you need the #Override annotation at the toString() method.
EDIT:
#Override <- add this
public String toString()
{
return this.studentName);
}
How would I develop the driver class for this code ive written ?
Array Class:
import java.util.Scanner;
public class Array
{
Scanner sc = new Scanner(System.in);
private double[] array = new double[];
public void setArray(double[] arr)
{
//I must set a value for the array length. set by user.
//user must input data
}
public boolean isInIncreasingOrder()
{
//must test if input is in increasing order
}
public boolean isInDecreasingOrder()
{
//must test if input is in descending order
}
public double getTotal()
{
//must find the total of all input
//total +=total
}
public double getAverage()
{
//must calculate average
//average = total/array.length
}
}
I guess what I'm asking is what exactly do i call in the DriverClass and how do I do it.
Thanks
The simplest way to test a class is to have a "public static void main(String[] args)" method in the class itself.
In this "main" method, you first create an instance of the class, and then call the various methods in the class, and verify that they do what you expect. To make testing easier, you might want to print out a message after each call to the class under test, showing the expected result, the actual result, and a friendly "OK" or "FAIL" to let you see easily if the method did what you wanted.
Example:
class MyClass {
private int x = 0;
public int getX() { return x;}
public void setX(int x) { this.x = x; }
public static void main(String[] args) {
MyClass instance = new MyClass();
instance.setX(42);
int value = instance.getX();
System.out.print("Expected 42, got "+value);
if (value == 42) {
System.out.println("OK");
}
else {
System.out.println("FAIL");
}
}
}
Once you're familiar with this approach to testing, you might look into unit test frameworks such as JUnit, which provide better ways to "assert" that a particular test is passing, and to understand the results of your testing.
public class QuestionBank {
public static void main(String[] args) {
int k = 0;
String Bank[][] = {{"The sun is hot.","A. True","B. Flase","A"},
{"Cats can fly.","A. True","B. False","B"}};
}
}
Above is my QuestionBank class that creates a 2X4 string array. First column being the question, 2nd and 3rd being the answer choices, and 4th being the correct answer.
Below is my RealDeal class.
import javax.swing.JOptionPane;
import java.util.Scanner;
public class RealDeal {
public static void main(String[] args) {
input = JOptionPane.showInputDialog(Bank[0][0]\nBank[0][1]\nBank[0][2]);
if (input == Bank[0][3]) {
input = 10;
} else {
input = 0;
}
total = input/1;
JOptionPane.showMessageDialog(null,"You scored a " + total + " out of 10. Great job!");
System.exit(0);
}
}
What I'm trying to do is to get Bank[0][0], Bank[0][1], and Bank[0][2] to output on my RealDeal class and then to check whether Bank[0][3] matches with the users input. Can anyone please help me with this. Im really new to java so if anyone could actually draw out the answer and explain it to me that would be great.
I think the best way is reading a good Java book and become familiar with the language itself and then try to solve this by your own. If you then have a real question there is no problem asking it here again. But your code is... not really working at all.
I don't think this portal is a "please do my work for me" portal.
To call anything from another class you will need to either setup a method for a return or make the variables public.
So:
public class Class1
{
// for method 1
public String s1 = "This is a string"
// for method 2
public Class1 {}
public returnString()
{
return s1;
}
}
public class CLASS2
{
public static void main(String args[])
{
// get the class
cls1 = new Class1();
// retrieving - method 1
String str = cls1.s1;
// retrieving - method2
str = cls1.returnString();
}
}