my problem is confusing me because I have done this before and it worked in previous programs but this particular program will not work. I just need to use the method typeInput and popularityNumber with the 2 variables I passed to them but I cannot call them in my main method without error. Either a ")" or ";" expected occurs, and it looks to me like there are parenthesis and semi colons where needed. I'm sure it's a quick fix and would appreciate learning how to fix it. Thank you!
public static void main(String[] args) {
// TODO code application logic here
nameInput();
typeInput(Scanner keyboard, CartoonStar star);
popularityNumber();
}
/**
*
* #param name
*/
public static void nameInput() {
System.out.println("What is the name of the cartoon character : ");
Scanner keyboard = new Scanner(System.in);
CartoonStar star = new CartoonStar();
String name = keyboard.next();
star.setName(name);
}
public static void typeInput(Scanner keyboard, CartoonStar star){
System.out.println("What is the cartoon character type: 1 = FOX,2 = CHICKEN,3 = RABBIT,4 = MOUSE,5 = DOG,\n"
+ "6 = CAT,7 = BIRD,8 = FISH,9 = DUCK,10 = RAT");
switch (keyboard.nextInt())
{case 1 :
star.setType(CartoonType.FOX);
break;
case 2 :
star.setType(CartoonType.CHICKEN);
break;
case 3 :
star.setType(CartoonType.RABBIT);
break;
case 4 :
star.setType(CartoonType.MOUSE);
break;
case 5 :
star.setType(CartoonType.DOG);
break;
case 6 :
star.setType(CartoonType.CAT);
break;
case 7 :
star.setType(CartoonType.BIRD);
break;
case 8 :
star.setType(CartoonType.FISH);
break;
case 9 :
star.setType (CartoonType.DUCK);
break;
case 10 :
star.setType(CartoonType.RAT);
break;
}
}
public static void popularityNumber(Scanner keyboard, CartoonStar star){
System.out.println("What is the cartoon popularity number?");
int popularity = keyboard.nextInt();
star.setPopularityIndex(popularity);
System.out.println(star.getName() + star.getType() + star.getPopularityIndex());
}
}
CartoonStar class (just in case you want it):
public class CartoonStar {
private String name;
private CartoonStar.CartoonType type;
enum CartoonType {
FOX(1),CHICKEN(2),RABBIT(3),MOUSE(4),DOG(5),CAT(6),BIRD(7),FISH(8),DUCK(9),RAT(10);
private final int animalType;
private static Map <Integer, CartoonType> map = new HashMap <Integer, CartoonType>();
private CartoonType(int animalType){
this.animalType=animalType;
}
public int getAnimlType(){
return animalType;}
}//enum types
private int popularityIndex; //1 to 10 10 being the most popular
public CartoonStar() {
}//end no argument construtor
public CartoonStar(String name,CartoonStar.CartoonType type, int popularityIndex) {
setName(name);
setType(type);
setPopularityIndex(popularityIndex);
}//end full constructor
//getters and setters
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setType(CartoonStar.CartoonType type) {
this.type = type;
}
public CartoonStar.CartoonType getType() {
return type;
}
public void setPopularityIndex(int popularityIndex){
this.popularityIndex = popularityIndex;
}
public int getPopularityIndex(){
return popularityIndex;
}
}
In your main method, you call your method as follows:
typeInput(Scanner keyboard, CartoonStar star);
The typeInput method expects a declared keyboard and declared star. You have called it incorrectly.
Your best option will be as follows:
public static void main(String[] args) {
// TODO code application logic here
nameInput();
popularityNumber();
}
public static void nameInput() {
System.out.println("What is the name of the cartoon character : ");
Scanner keyboard = new Scanner(System.in);
CartoonStar star = new CartoonStar();
String name = keyboard.next();
star.setName(name);
typeInput(keyboard, star);
}
I added typeInput() in your nameInput() method and removed it from the main() method.
Related
I want to print the default private bloodtype and rhfactor which is O+ and + I want to print it from another class which has the main method.
I've already tried creating new objects and printed them but still it says that I'm accessing a private variable. When you input something on the scanner it prints it but if you input none I want to print the constructor with private variables!
public class blooddata {
private String bloodtype;
private String rhFactor;
blooddata(){
bloodtype = "O";
rhFactor = "+";
}
blooddata(String btx, String rhx){
this.bloodtype = btx;
this.rhFactor = rhx;
}
public String getblood (String bloodtype){
return bloodtype;
}
public String getfactor (String rhFactor){
return rhFactor;
}
public void setblood(String bloodtype){
this.bloodtype = bloodtype;
}
public void setfactor(String factor){
this.rhFactor = factor;
}
}
here is the class that has main method
import java.util.Scanner;
public class Runblooddata {
static Scanner sc = new Scanner(System.in);
static String btx;
static String rhx;
public static void main(String[] args) {
System.out.print("Enter blood type: ");
btx = sc.nextLine();
System.out.print("Enter rhFactor: ");
rhx = sc.nextLine();
if (btx.isEmpty() || rhx.isEmpty()){
blooddata asd = new blooddata(); //this is where i am lost
}else{
blooddata bd = new blooddata();
bd.setblood(btx);
bd.setfactor(rhx);
System.out.println(bd.getblood(btx));
System.out.println(bd.getfactor(rhx));
}
}
}
Getters aren't supposed to have parameters.
When you declare a method parameter named the same as a field, the parameter hides the field. You basically return the parameter you take.
6.4.1. Shadowing
A declaration d of a field or formal parameter named n shadows, throughout the scope of d, the declarations of any other variables named n that are in scope at the point where d occurs.
public String getBlood() {
return bloodtype;
}
public String getFactor() {
return rhFactor;
}
I will help you to simplify it a bit.
final class Example {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter blood type: ");
String btx = sc.nextLine();
System.out.print("Enter rhFactor: ");
String rhx = sc.nextLine();
BloodData data = btx.isEmpty() || rhx.isEmpty() ?
new BloodData() :
new BloodData(btx, rhx);
System.out.println(data.getBloodType());
System.out.println(data.getRhFactor());
}
}
final class BloodData {
private final String bloodType;
private final String rhFactor;
BloodData() {
this("O", "+");
}
public BloodData(String bloodType, String rhFactor) {
this.bloodType = bloodType;
this.rhFactor = rhFactor;
}
public String getBloodType() {
return bloodType;
}
public String getRhFactor() {
return rhFactor;
}
}
As Andrew has already explain the design issue what you have in your code, I'll guide you towards the solution what you are seeking for.
blooddata bd = new blooddata();
if (!btx.isEmpty() && !rhx.isEmpty()){
bd.setblood(btx);
bd.setfactor(rhx);
}
System.out.println(bd.getblood());
System.out.println(bd.getfactor());
Hi I'm currently doing this program for my assignment to create like a virtual shop. Currently I'm using NetBean IDE 8.1 and got an error when I tried to test the program. It's said , even though the program said build successful when I tried to build the program.
I did tried to follow the instruction in this video but came out empty image here
Here is my main program ( I haven't complete the program yet but this error keeps blocking me for testing it)
import java.util.Scanner;
public class Zarashop {
int choice;
public static void main(String[] args, int clothA){
Scanner absorb = new Scanner(System.in);
// phase 1 login
cloth cl = new cloth(); // declaring objects
payment pay = new payment();
personalinfo pi = new personalinfo();
shipping ship = new shipping();
receipt re = new receipt();
System.out.println("Welcome to Zara's cloth Shopping Center");
System.out.println("\nThis an online Shopping application will help you to buy "+ "a new cloth");
System.out.println("Please enter your detail");
System.out.println("\nPlease enter your name");
String Name = absorb.nextLine(); // user input name
System.out.println("\nAre you a student? (yes) or (no)");
String personalchoice = absorb.nextLine(); // user input status
//phase 2
System.out.println("please choose your cloth" );
System.out.println("cloth A detail : size: 170cm and red color ");
int cloathA = absorb.nextInt();
System.out.println("enter quantity ");
int quantity = absorb.nextInt();
pay.setclothA(clothA); // store value
pay.setquantity(quantity);
// phase 3 payment
System.out.println("please press 1 to calculate your payment");
int choice = absorb.nextInt();
if(choice == 1){
pay.total();
}
else {
System.err.println("error!");
}
}
}
Here is my main class for cloth ( misspelled was intended)
public class cloth {
// superclass
private int quantity; //
private int clothA=200;
//void
void setclothA(int ca){
clothA = ca;
}
void setquantity(int q){
quantity=q;
}
//get
int getclothA(){
return clothA;
}
int getquantity(){
return quantity;
}
}
my main class for personalInfo
public class personalinfo {
// superclass
public String Name;
private int Password;
private String TypeCard;
private int CardNo;
private String Address;
private int TeleNo;
//void
void setName(String n){
Name = n;
}
void setPassword(int p){
Password = p;
}
void setTypeCard(String tp){
TypeCard = tp;
}
void setCardNo ( int cn){
CardNo=cn;
}
void setAddress ( int a){
CardNo=a;
}
void setTeleNo ( int tl){
TeleNo=tl;
}
//get
String getName(){
return Name;
}
String getAddress(){
return Address;
}
int getPassword(){
return Password;
}
String getTypeCard(){
return TypeCard;
}
int getCardNo(){
return CardNo;
}
int getTeleNo (){
return TeleNo;
}
}
my sub class for payment
package zarashop;
//subclass
public class payment extends cloth {
String Status = "Initial";
public void total(){
int ca = super.getclothA(); //fetching values
int q = super. getquantity();
int total= q*ca;
}
}
public class receipt extends shipping{
}
my sub for shipping
public class shipping extends payment {
public int typeofshipping;
//void
void settypeofshipping(String ts){
String typeofshipping = ts;
}
int gettypeofshipping(){
return typeofshipping;
}
}
subclass for receipt (i'm reserving this for the program to display all the necessary user input)
public class receipt extends shipping{
}
thank you everyone and sorry for my bad program and my English.
A Java application can accept any number of arguments from the command line, and all of those are interpreted as String, that's why main only takes an array of String as parameter.
Change you code to
public static void main(String[] args)
and you'll be able to launch your application.
If you need to support a numeric command-line argument, you must convert a String argument that represents a number to an int:
int firstArg;
if (args.length > 0) {
try {
firstArg = Integer.parseInt(args[0]);
} catch (NumberFormatException e) {
System.err.println("Argument" + args[0] + " must be an integer.");
System.exit(1);
}
}
parseInt throws a NumberFormatException if the format of args[0] isn't valid.
The example comes from official documentation.
Here is your mistake: public static void main(String[] args**, int clothA**)
Change it to public static void main(String[] args)
You will need to change your main method to public static void main(String[] args). By adding int clothA as a second argument, you are chaging the signature, and then it becomes an invalid main method to launch your application from.
If you want to retrieve something from console, you will find your argument inside args.
You can then retrieve clothA as follows:
int clothA;
if (args.length > 0) {
try {
clothA = Integer.parseInt(args[0]);
} catch (NumberFormatException e) {
System.err.println("Argument" + args[0] + " must be an integer.");
System.exit(1);
}
}
So I'm working on a (supposedly) simple java application that uses console inputs from a user, to change private variables in another class. Now I can change the value of the private variables in the EmpCls class directly from the main class by manually inputting a variable into the object, e.g.
EmpCls empObject1 = new EmpCls("josh"); but how do I get something like this
EmpCls empObject1 = new EmpCls(ctName); to work? where ctName is the variable that the user inputs. here's the relevant code from the main class:
import java.util.*;
public class NewWan {
static Scanner console = new Scanner(System.in);
public static void main(String[] args) {
EmpCls empObject1 = new EmpCls(ctName);
String ctName = empObject1.getName();
System.out.println("enter name: ");
ctName = console.next();
}
}
And the subclass in question:
public class EmpCls {
private String name;
private String ext;
private int yearStarted = 0;
public EmpCls()
{
}
public EmpCls(String inName)
{
this.name = inName;
}
public void setEmpDetails(String inName) //, String inExt, int inYearStarted)
{
this.name = inName;
// this.ext = inExt;
// this.yearStarted = inYearStarted;
}
public String getName()
{
return this.name;
}
public int getYearStarted()
{
return this.yearStarted;
}
public String getExt()
{
return this.ext;
}
public void displayDetails()
{
System.out.println("Name: " + name);
System.out.println("Ext: " + ext);
System.out.println("Year Started" + yearStarted);
}
}
some parts of the code are commented just to enable easier trouble shooting, other parts are part of a different problem im working on.
You just need to reorder the statements a bit and remove the one that doesn't make sense:
public static void main(String[] args) {
System.out.println("enter name: ");
String ctName = console.next();
EmpCls empObject1 = new EmpCls(ctName);
}
Hum... just to organize your code in the good way ? You use variable before getting value, and before declare it... Strange way ^^
public static void main(String[] args) {
System.out.println("enter name: ");
String ctName = console.next();
EmpCls empObject1 = new EmpCls(ctName);
System.out.println("You just enter " + empObject1.getName());
}
I am working on this code where a menu pops up and you enter a choice to enter a computer or to display the computers added. However the only problem i have is when it displays it give me a null for the type of cpu and its speed. It is suppose to display like this
\nBrandName:\tDell\n
CPU:\t\tpentium3,500HZ\n
Memory:\t\t398M\n
Price:\t\t$1,390.00\n\n
but it displays like this
\nBrandName:\tDell\n
CPU:\t\tnullHZ\n
Memory:\t\t398M\n
Price:\t\t$1,390.00\n\n
here is my code there are three classes a main Assignment4 class a CPU class and a computer class, I believe my error is somewhere in my computer class.
here is my Assignment4 class
// Description: Assignment 4 class displays a menu of choices to a user
// and performs the chosen task. It will keep asking a user to
// enter the next choice until the choice of 'Q' (Quit) is entered.
import java.io.*;
import java.util.*;
public class Assignment4
{
public static void main (String[] args)
{
// local variables, can be accessed anywhere from the main method
char input1 = 'Z';
String inputInfo;
String brandName;
double price;
int memory;
String cpuType;
int cpuSpeed;
String line = new String();
// instantiate a Computer object
Computer computer1 = new Computer();
printMenu();
//Create a Scanner object to read user input
Scanner scan = new Scanner(System.in);
do // will ask for user input
{
System.out.println("What action would you like to perform?");
line = scan.nextLine();
if (line.length() == 1)
{
input1 = line.charAt(0);
input1 = Character.toUpperCase(input1);
// matches one of the case statement
switch (input1)
{
case 'A': //Add Computer
System.out.print("Please enter the computer information:\n");
System.out.print("Enter a brand name:\n");
brandName = scan.nextLine();
computer1.setBrandName(brandName);
System.out.print("Enter a computer price:\n");
price = Double.parseDouble(scan.nextLine());
computer1.setPrice(price);
System.out.print("Enter a computer memory:\n");
memory = Integer.parseInt(scan.nextLine());
computer1.setMemory(memory);
System.out.print("Enter a cpu type:\n");
cpuType = scan.nextLine();
System.out.print("Enter a cpu speed:\n");
cpuSpeed = Integer.parseInt(scan.nextLine());
computer1.setCPU(cpuType, cpuSpeed);
break;
case 'D': //Display computer
System.out.print(computer1);
break;
case 'Q': //Quit
break;
case '?': //Display Menu
printMenu();
break;
default:
System.out.print("Unknown action\n");
break;
}
}
else
{
System.out.print("Unknown action\n");
}
} while (input1 != 'Q' || line.length() != 1);
}
/** The method printMenu displays the menu to a user**/
public static void printMenu()
{
System.out.print("Choice\t\tAction\n" +
"------\t\t------\n" +
"A\t\tAdd Computer\n" +
"D\t\tDisplay Computer\n" +
"Q\t\tQuit\n" +
"?\t\tDisplay Help\n\n");
}
}
here is my CPU class
public class CPU
{
private String type = "?";
private int speed= 0;;
public CPU(String type, int speed)
{
this.type = type;
this.speed = speed;
}
public String getType()
{
return type;
}
public int getSpeed()
{
return speed;
}
public void setType(String type)
{
this.type = type;
}
public void setSpeed(int speed)
{
this.speed = speed;
}
public String toString()
{
String result = this.type + "," + this.speed + "HZ";
return result;
}
}
and finally my Computer class
public class Computer
{
private String brandName;
private int memory;
private double price;
CPU Cpu;
public Computer()
{
brandName = "?";
memory = 0;
price = 0.0;
CPU Cpu = new CPU("?", 0);
}
public String getBrandName()
{
return brandName;
}
public CPU getCPU()
{
return Cpu;
}
public int getMemory()
{
return memory;
}
public double getPrice()
{
return price;
}
public void setBrandName(String BrandName)
{
brandName = BrandName;
}
public void setCPU(String cpuType, int cpuSpeed)
{
CPU cpu = new CPU(cpuType, cpuSpeed);
}
public void setMemory(int memoryAmount)
{
memory = memoryAmount;
}
public void setPrice(double price)
{
this.price = price;
}
public String toString()
{
String output = "\n"+"BrandName:"+"\t"+brandName+"\n"+
"CPU:\t\t"+Cpu+"HZ\n"+
"Memory:\t\t"+memory+"M\n"+
"Price:\t\t"+"$"+price+"\n\n";
return output;
}
}
You create a new CPU variable in the method setCPU, which gets destroyed when the method ends. It should instead change the instance variable Cpu, so that the information is retained.
You have make changes here:
Computer constructor:
CPU Cpu = new CPU("?", 0); `to` Cpu = new CPU("?", 0);
Computer's setCPU(String cpuType, int cpuSpeed)
CPU cpu = new CPU(cpuType, cpuSpeed); `to`
Cpu.setType(cpuType);
Cpu.setSpeed(cpuSpeed);
I want to show a list of animals by species. I make a method filterByS to do it, but it's just show the first pet of list.
public abstract class Pet {
protected String id;
protected String name;
protected double weight;
protected Date date;
public Pet(){}
public Pet(String ma, String ten, double trongLuong, Date ngayNhap) {
this.id = ma;
this.name = ten;
this.weight = trongLuong;
this.date = ngayNhap;
}
public abstract int Filter(int tmp);
//
public class Lion extends Pet {
private double meatED;
SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
public Lion(){}
public Lion(String ma, String ten, double trongLuong, Date ngayNhap,
double khoiLuongThit) {
super(ma, ten, trongLuong, ngayNhap);
this.meatED = khoiLuongThit;
}
public int Filter(int tmp) {
// TODO Auto-generated method stub
return tmp=2;
}
//
public class Monkey extends Pet {
private String food;
SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
public Monkey(){}
public Monkey(String id, String name, double weight, Date date,
String loaiTAYT) {
super(id, name, weight, date);
this.food = loaiTAYT;
}
public int Filter(int tmp) {
return tmp=1;
}
//
public class Snake extends Pet{
private double length;
SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
public Snake(){}
public Snake(String ma, String ten, double trongLuong, Date ngayNhap,
double chieuDai) {
super(ma, ten, trongLuong, ngayNhap);
this.length = chieuDai;
}
public int Filter(int tmp) {
// TODO Auto-generated method stub
return tmp=3;
}
in class PetManament i'm call method Filter to filter each species.
public class PetManament {
private List<Pet> listPet;
public PetManament() {
listPet = new ArrayList<Pet>();
}
public void filterByS(int k){
Iterator<Pet> iter=listPet.iterator();
while(iter.hasNext()){
Pet p=iter.next();
if(p.Filter(0)==k){
System.out.println(p.toString());
}
else if(p.Filter(0)==k){
return;
}
else if(p.Filter(0)==k){
return;
}
else return;
}
}
in class program i'm call filterByS.
public class Program {
static PetManament list=new PetManament();
public static void main(String[] args) {
int stepm=1;
do{
System.out.println("");
System.out.println("(1): Add new");
System.out.println("(2): Remove");
System.out.println("(3): edit pet information ");
System.out.println("(4): Search by id or name ");
System.out.println("(5): list");
System.out.println("(6): show species list");
System.out.println("(7): ");
System.out.println("(8): ");
Scanner s = new Scanner(System.in);
int step=s.nextInt();
switch(step){
case 1:
AddNew();
break;
case 2:
Remove();
break;
case 3:
break;
case 4:
TimKiem();
break;
case 5:
PrintL();
break;
case 6:
filterByS();
break;
case 7:
break;
case 8:
stepm=0;
}
}while(stepm==1);
}
private static void filterByS() {
Scanner s = new Scanner(System.in);
int k=0;
System.out.println("(1): show list monkey"+"\n(2) show list lion"+"\n(3)show list lion"+"\nback to menu");
k=s.nextInt();
switch(k){
case 1:
list.filterByS(k);
break;
case 2:
list.filterByS(k);
break;
case 3:
list.filterByS(k);
break;
default:
break;
}
}
That's because in filterByS in your PetManament class, if p.Filter(0)==k you print, you for whatever reason have a few of the same conditional following that, except returning in them (but they'll never be reached), but if that's not the case, you return.