I just wanted to know that why are final and abstract called as modifiers ,what is the essence of calling them as modifiers since there are two types of modifiers access modifiers and non-access modifiers so final and abstract come under the second category ,so why are these called as modifiers?
I'm having difficulty understanding this small piece of code:
package food; public abstract class Fruit { private String color; private String tasteType; public void setColor(String color) { this.color = color; } public abstract void setTasteType(String taste); }
The above is an abstract class which describes the basic structure that every fruit should "extend".
The below is a concrete subclass of the Fruit class called Apple.
import food.Fruit; class Apple extends Fruit { public void setTasteType(String taste) { tasteType = taste; } }
Also do note that the two pieces of code are in different packages!
Upon compiling the Apple class I get the following error: Apple.java:4: error: tasteType has private access in Fruit tasteType = taste; ^
What I don't understand is this: I've given a non-abstract implementation to the "setTasteType" method in the Apple class and clearly setTasteType should have the authority to modify the private instance variables of Fruit. But it turns out I'm wrong.
i am trying to write a coin program:Write a class called Coin which will be used by a program called CountFlips.
1. The class, is an object created from this program. It is composed of data values and methods. It should contain a main method and should output the number of flips done (an integer that will be read from the user), the number of heads and tails that occur.
2. The CountFlip class will flip a coin multiple times and counts the number of ‘heads’ and ‘tails’ that result. This class should have a method called flip() of type void; a method called isHead() of type Boolean and a toString() method that will return the current face of the coin as a string.
So i created 2 classes, one called Coin.java & the other CountFlips.java,
PHP Code: package test; public class Coin { private final int heads = 0; private final int tails = 1; private int facetype; private int flips;
Does a variable have public access modifier? if we can use it within the class and outside of the class then can i access a public variable as follows??
class mo { void display() { public int a=9; System.out.println(a); } public static void main(String[] args) { mo m=new mo(); m.display(); } }
ERROR: It shows 6 errors :-O. Error 1. illegal start of the expression 2. class,interface, or enum expected
"A constructor cannot be abstract, static, final, native, or synchronized."
I understand on why it can't be all of the above, except "final".
Why can't we have a final constructor, i understand constructors are not inherited, hence no chance/case of overriding etc. But why is it not allowed at all ?
I am working on a project that uses a custom tag in a JSP file. Here is the tag descriptor
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN" "http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd"> <taglib> <tlibversion>1.0.0</tlibversion> <jspversion>1.1</jspversion>
[code]...
Everything seems working fine except that the custom tag handler does not get called at call. I set breakpoint and it never stops there. The JSP page is displayed fine, only not showing the custom tag content and there is no logged error/warning from the log. The tld is found fine. Like what classes called/validated the tag handler so I can logging those classes at DEBUG level to see what goes wrong.
While studying polymorphism , i have some doubts that i am unable to clarify ..... Here they are :
Suppose our inheritance ladder be : C1 <- C2 <- C3 <-....... <- C100 where C1 is most general (superclass) and C100 is most specific class . Now , if i write java code in my main() :
C21 Obj = new C100(); Obj.someMethod();
So, what will happen in scenarios as given below :
Scenario - 1) If someMethod() is only defined in C1 ? How will compiler search for this someMethod() ?Will it be executed ? Scenario - 2) If that someMethod() is static and only defined in C1 , then how will it be searched and executed ? Scenario - 3) If someMethod() is only present in C100 , then what will happen ?
So, I have a gameOver() method that should when oval goes out of bounds abort game but, as soon as I start the game it runs the gameOver method. I've been looking over it for a while trying different things. I think what stood out to me is removing the abort sequence the game runs mostly as it should after, popup is closed and that if I replace game.gameOver(); with ya = -1 the ball bounces off the wall.
From within that thread I try to call a method from within the same class the contain the thread. This method is not being called. I have confirmed that that section of code is being executed in the thread.
If you look at the following code, the thread being run is called MainThread ln 114, and the method it calls that is not being executed is called onprogressTime() ln 105;
In my pursuit to modify the tutorial Android application Bluetooth Chat, I'm trying to add View Switcher to alternate between screens simultaneously in the app, but in my function onCreate ViewSwitcher isn't being resolving and I get a debugging error when trying to run the app. The spelling match and all required libraries are being called, so not sure how to fix it. My code for onCreate function is:
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (D) Log.e(TAG, "+++ ON CREATE +++");
// Set up the window layout setContentView(R.layout.main);
I am unable to understand the meaning of this sentence "final reference variables must be initialized before the constructor completes.",What is trying to imply?
why using the get method(c.get(c.HOUR_OF_DAY)); gives me the correct hour(currently 19) but directly accesing c.HOUR_OF_DAY returns 11 ? It shows In the documentation that HOUR_OF_DAY is public.
import java.util.*; public class calendar2 { public static void main(String[] args) { new calendar2().timer(); } private void timer() { Calendar c=Calendar.getInstance(); //c.clear(); System.out.println(c.get(c.HOUR_OF_DAY)); System.out.println(c.HOUR_OF_DAY);
How can i take run time value for static final variable...my lecturer said first time assignment is possible for declared final variable but in my case it shows compile time error..I'm placing my program below with error message
class Sample { static final String cname; void print() { System.out.println(cname); } public static void main(String args[]) { cname=args[0]; Sample s=new Sample(); s.print(); } }
Sample.java:11: cannot assign a value to final variable cname. cname=args[0];
For this assignment you will be writing a grade book program. The program will work for one student. It will need to take as input the students name. The user will then be asked to input grades into three categories in this order:
1) homework; 2) quizzes; 3) tests.
The grades in a given category will be averaged to one number that is the average of all grades in that category. The final average will be the weighted average of each category, where homework is worth 25% quizzes are worth 25%, and test are worth 50%. Like this:
Homework Grades: 65, 70, 75, 80, 80 Homework Average: 74 Quiz Grades: 75, 80, 85, 80. Quiz Average: 80 Test Grades: 75, 80, 85, 75 Test Average: 78.75 Final Average = 0.25*HomeworkAvg + 0.25*QuizAvg + 0.50*TestAvg = 77.87
But i only have the average and i dont know how to move past that..
Heres my average code :
import java.util.Scanner; public class Homework3 { public static void main(String[] args) { Scanner myScanner = new Scanner(System.in); int gradeCount = 0; int grades = 0; int holder = 0;
Im working on a class to add to my final project but im getting a parsing error for the final bracket, i double checked to make sure im not missing any but im still getting an error
import java.util.Scanner; public class totalprice { public static void main(String[] args) { scanner keyboard = new Scanner(System.in); { DecimalFormat num = new DecimalFormat("#.00"); char meal; int ammount; double cost;
While reading head first java i encountered a problem(Pg. 90 chapter 4 - mixed messages).
Suppose in a class(say A) outside main() a counter variable is declared and initialized to 0.
In main() declared the array of objects of the class A.
Consider a while loop in which we increment the counter as follows:
public class A{ int counter = 0; public static void main(String[] args){ A[] arr = new A[20]; int x = 0; while(x<4){ arr[x] = new A(); //arr[] is array object arr[x].counter += 1; x++; } } };
what is the final value of counter ? will it be the same for all array objects.