0

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

6
Contributors
21
Replies
22
Views
6 Years
Discussion Span
Last Post by tong1
0

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

0

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

Edited by leiger: n/a

0

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);
    }
}
0

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?

0

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

0

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

Edited by NormR1: n/a

0

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

0

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

Please try to write the code you need.

0

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);
    }
}
0

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

Edited by NormR1: n/a

0

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);
    }
}
0

NormR1 Do you know anything about Linked list??

0

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());
	}

}
0

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());
	}

	
}
0
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.");

    }
}

Edited by Nick Evan: Fixed formatting

0
/*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.");

    }
}

Edited by Reverend Jim: Fixed formatting

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.