| CodeGuru Home | VC++ / MFC / C++ | .NET / C# | Visual Basic | Newsletters | VB Forums | Developer.com |
|
|
|||||||
| Java Programming Ask your Java programming question and help out others with theirs. |
![]() |
|
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
so here is the big problem... the point of this assignment was to make a program that accepts 2 arguments of which only the first letter of each is read so i understand i would have to use the .charAt method to read Them
after it reads the strings and accepts the char values it is supposed to switch them out for strings after it has done that it must check the validity of the switch and output a message saying either ("your size is: +checksize") or ("invalid size") i understand that part so here is the point of my problem i dont know how to get my argument that i inputed into my checkorder class from my orderform class? both the orderform and the check order use my pizza class. here are my three classes so far... p.s. i have also included the slides where i was given instructions on what to do in case you can not understand what i said... Code:
/**
* Write a description of class Pizza here.
*
* @author Michael Wojtysiak
* @version 1.1
*/
public class Pizza
{
private String size;
private String toppings;
private static int PizzasSold;
public Pizza()
{
System.out.println("Pizza One - Default Constructor");
setSize("Small");
setToppings("Regular");
System.out.println("Size: " + size);
System.out.println("Toppings: " + toppings);
System.out.println("");
}
public Pizza(String setSize, String setToppings)
{
this.size = setSize;
this.toppings = setToppings;
System.out.println("Pizza Three - Two Parameter Constructor");
System.out.println("Size: " + size);
System.out.println("Toppings: " + toppings);
System.out.println("");
}
public Pizza(String setSize)
{
setSize("Medium");
setToppings("Regular");
System.out.println("Pizza Two - One Parameter Constructor");
System.out.println("Size: " + size);
System.out.println("Toppings: " + toppings);
System.out.println("");
}
public void setSize(String size)
{
this.size = size;
}
public void setToppings(String toppings)
{
this.toppings = toppings;
}
public String getSize()
{
return this.size;
}
public String getToppings()
{
return this.toppings;
}
public static int getPizzasSold()
{
return PizzasSold;
}
// public boolean equal(Pizza CompPizza)
// {
// if(this.getSize()==(CompPizza.getSize()))
// {
// if(this.getToppings()==(CompPizza.getToppings()))
// {
// return true;
//}
// else
// {
// return false;
// }
// }
// return false;
// }
public boolean equal(Pizza CompPizza)
{
if(this.getSize()==(CompPizza.getSize()))
{
if(this.getToppings()==(CompPizza.getToppings()))
{
return true;
}
else
{
return false;
}
}
return false;
}
private void calculatePrice()
{
}
public void display(String size, String toppings, double price)
{
System.out.println("Size: " + size);
System.out.println("Toppings: " + toppings);
System.out.println("Price: " + price);
}
}
Code:
import java.util.Scanner;
/**
*Has Pizzas
*
* @author Michael Wojtysiak
* @version 1.1
*/
public class OrderForm
{
public static void main(String args[])
{
String mySize = args[0];
String myToppings = args[1];
CheckOrder.checkSize(args[0]);
// CheckOrder.checkTopping(args[1]);
Pizza One = new Pizza();
Pizza Two = new Pizza(mySize);
Pizza Three = new Pizza(mySize,myToppings);
boolean Res = Three .equal(One);
if(Res == true)
{
System.out.println("They are the same!");
}
else
{
System.out.println("They are different!");
}
}
}
Code:
/**
* Write a description of class CheckOrder here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class CheckOrder
{
public static boolean checkSize(String size)
{
char num = size;
String checksize;
switch(num)
{
case 's' : checksize = "Small";
break;
case 'm' : checksize = "Medium";
break;
case 'l' : checksize = "Large";
break;
default : checksize = "Invalid";
}
if ((checksize == "Small")||(checksize == "Medium")||(checksize == "Large"))
{
System.out.println("Your Size is: " + checksize);
}
else
{
System.out.println("Invalid Size");
}
}
//if(boolean checkSize = true)
//{
// size = "Small";
// }
//else
//{
// System.out.println("Invalid");
// }
}
|
|
#2
|
||||
|
||||
|
Re: Java Pizza Program Help
The code itself makes little sense, but anyways...
The things I saw in the first 5 seconds: error#1: char num = size; You can't initialize char with a string directly. One of many ways to do it would be char num = size.charAt(0); considering you are sure there is at least one char in the string. error#2: if ((checksize == "Small")||(checksize == "Medium")||(checksize == "Large")), this.getSize()==CompPizza.getSize() and more... That's a nice one =). This is no JavaScript, you can't compare objects' contents the way you do with primitives. Use equals() method for this (i.e. str1.equals(str2)). And I am not even talking about algorithm corrections/optimizations... The program will throw basic exceptions almost every line if something goes wrong. Check status of one step before starting the next one.
__________________
Wanna install linux on a vacuum cleaner. Could anyone tell me which distro sucks better? willCodeForFood("PHP,Java,C#,C++,Assembler,XML,VBS,XHTML,CSS,JavaScript,SQL"); //always looking for job opportunities in AU/NZ/US/CA/Europe :P USE [code] TAGS! Read this FAQ if you are new here. If this post was helpful, please rate it! Last edited by Xeel; November 6th, 2009 at 07:16 PM. |
|
#3
|
|||
|
|||
|
Re: Java Pizza Program Help
ok number 1 was delt with what do you suggest about error 2... and ye the codes probably incorrect in many ways but please note i am a first year college student with no background in any programming and i am only a month in my object oriented java 1 program
|
|
#4
|
|||
|
|||
|
Re: Java Pizza Program Help
Quote:
Code:
String s = null;
if(s.equals("Something")) {
// NullPointerException
}
if("Something".equals(s)) {
// Will never NPE
}
__________________
------ If you are satisfied with the responses, add to the user's rep! |
![]() |
| Bookmarks |
| Tags |
| pizza |
|
||||||
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|