Hello,
I am trying to display the output as "1(10) 2(23) 3(29)" but instead getting output as "1 2 3 (10)(23)(29)". I would be grateful if someone could have a look the code and possible help me. I don't want to use arraylist.
the code this
// int[] Groups = {10, 23, 29}; in the constructor
public String toString()
{
String tempStringB = "";
String tempStringA = " ";
String tempStringC = " ";
for (int x = 1; x<=3; x+=1)
{
tempStringB = tempStringB + x + " ";
}
for(int i = 0; i < Group.length;i++)
{
tempStringA = tempStringA + "(" + Groups[i] + ")";
}
tempStringC = tempStringB + tempStringA;
return tempStringC;
}
Should I point out that I am a begginer at this?
double averageMonthlyTemp() {
double[] amt = new double[52];
int sum = 0;
int index = 0;
for (int i = 0; i < temp.length - 1; i = i + 7) {
//where temp is an existiing
//previously initialized array
//of 365 elements, form 0 to 364
for (int j = 0; j < 7; j++) {
sum = sum + temp[i + j];
if (j % 7 == 6) {
double average = ((double) sum) / 7;
amt[index] = average;
index++;
sum = (int) 0;
}
}
}
return amt;
}
When I try to compile, I get an "incompatible types" error, with the "amt" at return amt marked in red. Does somebody know why?
i am working on a webapp to download videos from streaming websites.
i don't know anything about applets but stuck here to know about them.
anybody please give basic detailed method to create such applet and how to use it?
Please see code snippet:
File[] additionalFiles = new File(FILE_PATH).listFiles();
boolean isDirectory = file.isDirectory();
I have verified that the directory path is correct, and when I run the code on Windows, the value of isDirectory is true (as it should be). Any suggestions as to why this occurs on Linux (RedHat Enterprise Linux)?
I am trying to compute (360 / 24) / 60 I keep getting the answer 0.0 when I should get 0.25
In words: I want to divide 360 by 24 and then divide the result by 60
public class Divide {
public static void main(String[] args){
float div = ((360 / 24) / 60);
System.out.println(div);
}
}
This prints out:
0.0
Why is that? Am I doing something really stupid, or is there a good reason for this
Hello,
Say i have
List<SomeObject> objList = new ArrayList<SomeObject>();
If someObject contains a field named id. Can we find it through some query like
objList.filter('id=2');
wihout looping through the list? If not, then why? This can be so useful method and used as an alternative to write tedious for loop?
Just a question that came to my mind so i thought i must clear it here :)
Thanks in advance :)
class Parent
{
private void method1()
{
System.out.println("Parent's method1()");
}
public void method2()
{
System.out.println("Parent's method2()");
method1();
}
}
class Child extends Parent
{
public void method1()
{
System.out.println("Child's method1()");
}
}
class test {
public static void main(String args[])
{
Parent p = new Child();
p.method2();
}
}
I'm confuse why does in Parent::method2() when invoking method1() it will cal Parents method1() and not Childs method1 ? I see that this happens only when method1() is private? Can someone explain me why ?
Thanks you.
I am working on a recursive insertion method for a BST. This function is suppose to be a recursive helper method and is in a private class called Node. The Node class is in a class called BinarySearchTree which contains an instance variable for the root.
When I am trying to insert an element, I get a NullPointerException at :
this.left = insert(((Node)left).element);
I am unsure about why this occurs. If I understand correctly, in a BST, I am suppose to insert the item at the last spot on the path transversed. Any help is appreciated!
private class Node implements BinaryNode<E>
{
E item;
BinaryNode<E> left, right;
public BinaryNode<E> insert(E item)
{
int compare = item.compareTo(((Node)root).item);
if(root == null)
{
root = new Node();
((Node)root).item = item;
}
else if(compare < 0)
{
this.left = insert(((Node)left).item);
}
else if(compare > 0)
{
this.right = insert(((Node)right).item);
}
return root;
}
}
I get headaches when I have to write nearly 10 lines of code to say 2 Objects are equal, when their type is equal and both's attribute is equal. You can easily see that in this way of writing the number of lines increase drastically with your number of attributes.
public class Id implements Node {
private String name;
public Id(String name) {
this.name = name;
}
public boolean equals(Object o) {
if (o == null)
return false;
if (null == (Id) o)
return false;
Id i = (Id) o;
if ((this.name != null && i.name == null) || (this.name == null && i.name != null))
return false;
return (this.name == null && i.name == null) || this.name.equals(i.name);
}
}
defined variable:
LinkedList list1=new LinkedList();
Object get() in list1 obtains a node of list1
Object remove() in list1 deletes a node of list1
count() is length of list1
for(int i=1;i<list1.count();i++){
if(list1.get(i).startsWith('"',0)) //Error here
list1.remove(i);
}
Error: cannot find symbol
symbol: method charAt(int)
location: class Object
how to fix this problem?
I would like to delete the node in list1 which starts with (").
Hello All,
I have a following ArrayList,
[Title,Data1,Data2,Data3]
[A,2,3,4]
[B,3,5,7]
And I would like to convert this one like this,
[Title,A,B]
[Data1,2,3]
[Data2,3,5]
[Data3,4,7]
I'm bit confused with the approach. Any hint would be much appreciated.
Thanks.
Why doesn't the following program return 0, since I am accessing p from a new A(), which has not had main called on it?
public class A {
public int p = 0;
public static void main(String[] args) {
p = Integer.parseInt(args[0]);
new B().go();
}
}
class B {
public void go() {
System.out.println(new A().p);
}
}
Why is it that
class swi22
{
public static void main(String[] args)
{
int a=98;
switch(a)
{
default:{ System.out.println("default");continue;}
case 'b':{ System.out.println(a); continue;}
case 'a':{ System.out.println(a);}
}
System.out.println("Switch Completed");
}
}
gives error as: continue outside of loop
in c or c++
function comlen is defined such
int comlen(char *p,char *q){
int i=0;
while *p && (*p++==*q++)
i++;
return i;
is this code equivalent of this function
int comlen(String s,String m){
int i=0;
while (i<s.length() && s.charAt(i)==m.charAt(i)){
i++;
}
return i;
?
please help
I have following Class, I need to get type in constructor, how can I do that?
public abstract class MyClass<T> {
public MyClass()
{
// I need T type here ...
}
}
Lets us take instances of two classes
public abstract class Shapes
{
public abstract void draw(Graphics g);
}
public class Rectangle extends Shapes
{
public void draw(Graphics g)
{
//implementation of the method
}
}
here the class Rectangle has extended class Shapes and implicitly it extends class Object.I know no other extension is possible but cant we call inheriting classes Shapes and Object multiple inheritance?(Since inheriting two classes is multiple inheritance from one perspective)
I wrote paging logic:
My requirement: total elements to display:100 per page,if i click next it should display next 100 records,if i click previous 100 records.
Initial varaible values:
showFrom:1,
showTo:100
max elements:depends on size of data.
pageSize:100.
Code:
if(p*emphasized text*aging.getAction().equalsIgnoreCase("Next")){
paging.setTotalRec(availableList.size());
showFrom = (showTo + 1);
showTo = showFrom + 100- 1;
if(showTo >= paging.getTotalRec())
showTo = paging.getTotalRec();
paging.setShowFrom(showFrom);
paging.setShowTo(showTo);
}
else if(paging.getAction().equalsIgnoreCase("Previous")){
showTo = showFrom - 1;
showFrom = (showFrom - 100);
paging.setShowTo(showTo);
paging.setShowFrom(showFrom);
paging.setTotalRec(availableList.size());
}
Here i can remove and add the elements to the existing data.above code works fine if i add and remove few elements.but if i remove or add 100 elements at a time counts are not displaying properly above code works fine if i add and remove few elements.
Arghh I just know people are going to hate me for asking this...
I was just playing around with inheritance and I noticed you can instantiate a subclass object in one of two ways when you write code. So then I wondered if there is any functional difference between these two methods. So in the code below, does this produce the exact same result...a MountainBike object, or is there some difference I should know about? Bicycle is the superclass for this example. If I do Bicycle bike or MountainBike bike I am effectively making a MountainBike due to new MountainBike()? So basically the difference is just semantics at this point?
Bicycle bike = new MountainBike();
MountainBike bike = new MountainBike();