Hi! I need create a class to represent a "rock". When a rock object is created it should have print out a message to the console saying: "Rock 1 created". I should have a field that contains the rock's weight, methods to set and get the weight and method to roll a rock which causes the weight to decrease by 20%.

also I need write a test program that will create three rocks and set their weights to 10kg. The program will then "roll" rock 2 and then print out the weight of rock 2.

Best Regards

Yes, okay. So go and create it! ;)

Unless you had a specific question to ask?

good luck

I have no idea how to start. If you know please help me.

Okay, so you need two classes "Rock" and "TestRock" (second one has a main method)

Rock needs to have:
- field to hold the weight
- constructor(s)
- set method for weight, which accepts weight from caller
- get method for weight, which returns weight to caller
- roll method, which changes weight by a fixed amount

TestRock needs to have:
- main method, where you create the Rocks from and call it's methods from

Hi. can anybody help me to finish this programme. the perpuse of this programme is output number in word when you input number for example 1-one.

import java.math.*;
import java.util.*;


public class Homework2 {


    public static void main(String[] args) {
    	
        String n[] = {"zero","one", "two", "three", "four", "five", "six", "seven", "eight", "nine","ten"};        
        String x[] = {"twenty", "thirty", };

        Scanner in = new Scanner(System.in);
        while(true)
        {
            int num = in.nextInt();
            if(num == 0)
            {
                System.exit(0);
            }
            
            pr(num);
            if (num >= 20)
            {
            	if (num == 20) pr("twenty");
            	else
            	{
            		pr(x[num/10 -2]);
            		pr(n[num%10]);
            		
            	}
            }
            
            else
            {
            	xxx(num);
            }
            //
        }
    }
    
    static void pr(Object o)
    {
    	System.out.println(""+o);
    }
}

Can you explain what your problem is?
Show the program's output and explain where its wrong and what you want to change it.

Do you have any notes or a design on how you are going to solve the problem? Or some pseudo code?

Whoa, cowboy. One homework problem at a time!

NormR1. My problem is that I need outputs for numbers after 10 to 20. I tryed to it in different ways but its diesn't work. if you have any idea can you help me please

I think that in English the numbers from 10 to 20 are unique and will have to be handled one by one.
What did you try?

Comment on your code:
Use meaningful variable names, not single letters line x and n

can you do these little changes and show me you idea??

I'm here to help students learn programming.
You learn by trying.

Please try to write the code you need.

This is my code do outline variables in words when you input numbers. numbers from 20 to 90 works well, but integers from 1 to 20 doesn't work.. I dont know what i have to add exactly to my code.

import java.math.*;
import java.util.*;


public class Homework2 {


    public static void main(String[] args) {
    	
        String n[] = {"zero","one", "two", "three", "four", "five", "six", "seven", "eight", "nine","ten"};        
        String x[] = {"twenty", "thirty","fourty","fifty","sixty","seventy","eighty","ninty" };

        Scanner in = new Scanner(System.in);
        while(true)
        {
            int num = in.nextInt();
            if(num == 0)
            {
                System.exit(0);
            }
            
            pr(num);
            if (num >= 20)
            {
            	if (num == 20) pr("twenty");
            	else
            	{
            		pr(x[num/10 -2]);
            		pr(n[num%10]);
            		
            	}
            }
        }
 
        
    }
    
    static void pr(Object o)
    {
    	System.out.println(""+o);
    }
}

Use meaningful variable names, not single letters like x and n

Can you explain what this expression is supposed to do: num/10 - 2

/10 is integer arithmetic. That means 12/10 = 1 and 19/10 also = 1

done it)))

import java.math.*;
import java.util.*;


public class Homework2 {


    public static void main(String[] args) {
    	
        String n[] = {"zero","one", "two", "three", "four", "five", "six", "seven", "eight", "nine","ten"};
        String y[] = {"eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen","nineteen"};
        String x[] = {"twenty", "thirty","fourty","fifty","sixty","seventy","eighty","ninty" };

        Scanner in = new Scanner(System.in);
        while(true)
        {
            int num = in.nextInt();
            if(num == 0)
            {
                System.exit(0);
            }
            pr(num);
            if (num <=10)
            {
            	if (num ==10)pr("ten");
            	else
            	{
            		pr(n[num%10]);
            	}
            }
            
        
            
            pr(num);
            if (num >10 )
          
            {
            	
            	{
            		
            		pr(y[num%11]);
            		
            	
            
        
            
            pr(num);
             if (num >= 20)
            {
            	
            	if (num == 20) pr("twenty");
            	else
            	{
            		pr(x[num/10 -2]);
            		pr(n[num%10]);
            	}
            }
            }
        }
        }
            
        
    }
            		
            		
            	
            
    
 
        
    
    
    static void pr(Object o)
    {
    	System.out.println(""+o);
    }
}

NormR1 Do you know anything about Linked list??

Please start a new thread if you have questions on a new topic.

This is Test of my linked list

//Program Name 	


//Version History


public class TestMyLinkedList {

	public static void main(String[] args) {

		MyLinkedList mll = new MyLinkedList();
		

		
		mll.add("Hello");
		mll.add("world");
		mll.add("one");
		mll.add("two");
		mll.add("three");
		
		pr("");
		mll.start();
		pr(mll.get().toString());
		
		
		mll.next();
		pr("\n"+mll.get().toString());
		
		

		
		mll.start();
		mll.next();

		
		
		pr("");
		mll.print();
		
		
	}
	
	//a helper method to print
	public static void pr(Object o)
	{
		System.out.println(""+o.toString());
	}

}

and this is Linked list

public class MyLinkedList {
	
	Node start = null;
	Node current = null;
	
	public MyLinkedList()
	{
	}
	
	public void add(Object o)
	{
		Node nd = new Node();
		nd.data = o;
		nd.next = null;
		if (start == null)
		{
			start = nd;
		}
		else
		{
			current.next = nd;
		}
		current = nd;
	}
	
	public void start()
	{
		current = start;
	}
	
	public Object get()
	{
		return current.data;
	}
	
	public void next()
	{
		if (current.next != null)
		{
			current = current.next;
		}
	}
	
	public void print()
	{
		if (start != null)
		{
			current = start;
			while (current != null)
			{
				pr(current.data);
				current = current.next;
			}
		}
	}

	//inner class
	class Node
	{
		Object data;
		Node next;
	}
	
	//a helper method to print
	public static void pr(Object o)
	{
		System.out.println(""+o.toString());
	}

	
}

I need to do insert and remove

class Rock{
    double weight;
    static int total=0;

    public Rock(double w){
        setWeight(w);
        System.out.println("Rock " + ++total + " is created");
    }

    void roll(){
        weight *=1.2;
    }

    void setWeight(double d){   //成员方法
        weight = d>0?d:1;
    }
    double getWeight( ){
        return weight;
    }
}

public class RockTest{
    public static void main(String args[]){
    Rock rock[]= new Rock[3];
    for (int i=0; i<3; i++)
    rock[i]=new Rock(10);
    rock[1].roll();
    System.out.println("The rock 2 has " + rock[1].getWeight() + " kg in weight after rolling.");

    }
}
/*The rocks are classified into the following 3 groups in origin:
Sedimentary, Igneous, and Metaphorphic. */
class Rock{
    private double weight;
    private static int total=0;
    private String origin;

    public Rock(double w, String s){
        setWeight(w);
        System.out.println("Rock " + ++total + " is created");
        origin = s;
    }

    public void roll(){
        weight *=1.2;
    }

    public void setWeight(double d){   //成员方法
        weight = d>0?d:1;
    }
    public double getWeight( ){
        return weight;
    }
    public String getOrigin(){
        return origin;
    }
}

public class RockTest{
    public static void main(String args[]){
        String s[]={"Sedimentary","Igneous","Metamorphic"};
    Rock rock[]= new Rock[3];
    for (int i=0; i<3; i++)
    rock[i]=new Rock(10,s[i]);
    rock[1].roll();
    System.out.println("The " + rock[1].getOrigin() + " rock 2 has " + rock[1].getWeight() + " kg in weight after rolling.");

    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.